tkMapper usage and demo demonstration
1. Realization Law of Dao Layer - Preface
- Entity classes correspond to data tables and are regular - they can be generated as long as the structure of the data table is known.
A single word equals a single word, and multiple word fields are the hump nomenclature. - The methods defined in the DAO interfaces of all entities are also regular, except for the entity types.
UserDao
public interface UserDao{ public int insert(User user); public User queryOneByPrimartKey(int i); }
GoodsDao
public interface GoodsDao{ public int insert(Goods goods); public Goods queryOneByPrimartKey(int i); }
You can see that the difference between the two DAO s is the entity type.
So get a generic DAO template:
GeneralDao
//generic paradigm public interface Dao<T>{ //General method public int insert(T t); public T queryOneByPrimartKey(int i); }
Then let other interfaces inherit this generic DAO for higher development efficiency.
UserDao
public interface UserDao extends GeneralDao{ }
GoodsDao
public interface GoodsDao extends GeneralDao{ }
The two methods of universal DAO will naturally inherit from the past!
- The database operation method defined for the GeneralDao interface does not need to map files because it uses generics.
Mapping files are required for UserDao and GoodsDao, and the mapping files for all Daos with the same operations are regular.
UserDao
<insert id="insert"> insert into users(user_id,username) values (#{userId},#{username}) </insert>
@Table("users") public class User{ @Id @Column("user_id") private int userId; @Column("username") private String username; }
GoodsDao
<insert id="insert"> insert into goods(goods_id,goods_name) values (#{goodsId},#{goodsName}) </insert>
@Table("product") public class Goods{ @Id @Column("goods_id") private int goodsId; @Column("goods_name") private String goodsName; }
2. Introduction to tkMapper
Mybatis provides a number of third-party plug-ins that typically encapsulate data manipulation methods (General Dao example above), reverse engineering databases (generating entity classes from data tables, generating mapping files)
- MyBatis-plus
- tkMapper
tkMapper is a MyBatis plug-in that provides many tools based on MyBatis to simplify development.
- Provides a common database operation method for single tables. This helps us define a generic GenaralDao. (Joint table does not work)
- Reverse engineering (generating entity classes, Dao interfaces, mapping files from data tables)
3. tkMapper Integration
3.1 Integration of MyBatis based on SpringBoot
See project tkmapper-demo
- Create a boot project to integrate mybatis
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/zerofashion_boot?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 mybatis.type-aliases-package=com.yty.tkmapperdemo.beans mybatis.mapper-locations=classpath:mappers/*Mapper.xml
I'm done integrating MyBatis!
3.2 Integrating tkMapper
3.2.1 Add tkMapper dependencies
<!--tkMapper--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency>
3.2.2 Modify the Startup Class@MapperScan annotation
- The @MapperScan comment should be inside the tk package, not the org.
4. Use of tkMapper
4.1 Create entity classes
@Data @NoArgsConstructor @AllArgsConstructor @Table(name = "users") public class User { private int userId;//User id private String username;//User name private String password;//Password private String nickname;//Nickname? private String realname;//Real name private String userImg;//Head portrait private String userMobile;//Cell-phone number private String userEmail;//E-mail address private String userSex;//Gender private Date userBirth;//Birthday private Date userRegtime;//Registration Time private Date userModtime;//Update Time }
4.2 Create Dao Interface
tkMapper has completed the wrapping of common operations for forms (crud and crud by condition) - encapsulated in the Mapr interface and the MySqlMapper interface; So to operate on forms, you just need to customize Dao to inherit both interfaces
import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; public interface UserDao extends Mapper<User>, MySqlMapper<User> { }
Inherit the generic encapsulated interfaces Mapper and MySqlMapper (to be generic).
4.3 Test
Click UserDao and alt+insert to test Unit4.
@RunWith(SpringRunner.class) @SpringBootTest(classes = TkmapperDemoApplication.class) public class UserDaoTest { @Autowired private UserDao userDao; @Test //1. Add public void test1(){ User user = new User(); user.setUsername("curry"); user.setPassword("123123"); user.setUserImg("img/default.png"); user.setUserRegtime(new Date()); user.setUserModtime(new Date()); int i = userDao.insert(user); System.err.println(i); }
Result:
5. Methods provided by tkMapper
5.1 New Category Entity Class
@Data @NoArgsConstructor @AllArgsConstructor @Table(name = "category") public class Category { @Id @Column(name = "category_id") private int categoryId; //Classification id private String categoryName; //Category name @Column(name = "category_level") private int categoryLevel; //Classification Level @Column(name = "parent_id") //You don't want to use this column for the corresponding data table, just change int to Integer, because class reflection doesn't work with class reflection private int parentId; //id of the previous level category private String categoryIcon; //Category icon logo private String categorySlogan; //slogan private String categoryPic; //Classification Map private String categoryBgColor; //background color }