easymybatis is a mybatis enhanced class library, which aims to simplify the development of mybatis and make it more efficient.
- git address: easymybatis
- demo address: demo with springboot
easymybatis has the following characteristics:
- The CRUD operation can be completed without writing xml files.
- Support multi-table query, aggregate query and paging query (support multiple databases).
- Support batch addition, specify field batch addition.
- Support Dao layer access control, such as a Dao has only query function, a Dao has crud function and so on.
- Support for custom sql, SQL statements can be written in configuration files, as well as mybatis tags.
- Support mysql, sqlserver, oracle, other database expansion is convenient (add a template file can be).
- The mode of use remains unchanged, and integration with Spring has only changed one configuration.
- Support integration with spring boot.
- Mybatis parameter settings are flexible, inheriting the official settings of mybatis.
- Lightweight, non-intrusive, and can coexist with traditional mybatis usage.
- Without modifying the framework source code (no plug-ins), you can use the functionality provided by the government at the same time.
easymybatis supports the following functions:
- Basic CRUD
- Primary Key Policy Settings
- Field Filling Function
- Enumeration attribute
- Global Dao
- Optimistic lock
- Logical deletion
Architecture Composition
The architecture of easymybatis is as follows:
Operation process
The flow chart of easymybatis:
- easymybatis scans Dao.java when the server starts.
- After scanning, the Dao.class and Entity.class are resolved.
- The code generation component generates mapper file content according to Dao.class and Entity.class, and the generation method is specified by the velocity template.
- Set the content of the mapper file into a Resource object in SqlSessionFactory.
Quick start
Create a spring boot project
Visit http://start.spring.io/ Generate a spring boot empty project, enter Group, and generate Artifact points, as shown in Figure 1:
Click Generate Project to download demo.zip
Import project
Unzip the downloaded demo.zip and import the project. Right-click in eclipse - > Import... - > Existing maven Project, and select the project folder. Import into eclipse and wait for the maven related jar package to download.
Adding maven dependencies
When the jar package is downloaded, open pom.xml and add the following dependencies:
<!-- easymybatis Of starter --> <dependency> <groupId>net.oschina.durcframework</groupId> <artifactId>easymybatis-spring-boot-starter</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
Add database configuration
Add database configuration to application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=root
Add Java files
Assuming that there is a t_user table in the database, the DDL is as follows:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`username` varchar(255) DEFAULT NULL COMMENT 'User name',
`state` tinyint(4) DEFAULT NULL COMMENT 'state',
`isdel` bit(1) DEFAULT NULL COMMENT 'Delete',
`remark` text COMMENT 'Remarks',
`add_time` datetime DEFAULT NULL COMMENT 'Adding time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='User table';
We add the corresponding entity class and Dao:
- TUser.java :
@Table(name = "t_user")
public class TUser {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id; // ID
private String username; // User name
private Byte state; // state
private Boolean isdel; // Delete
private String remark; // Remarks
private Date addTime; // Adding time
// Omit getter setter
}
Entity class files are generated in the same way as hibernate. You can use our code generation tool to generate them. https://gitee.com/durcframework/easymybatis-generator
- TUserDao.java :
public interface TUserDao extends CrudDao<TUser> {
}
TUserDao inherits CrudDao, so that the Dao has CRUD functionality.
Adding test cases
public class TUserDaoTest extends DemoApplicationTests {
@Autowired
TUserDao dao;
@Test
public void testInsert() {
TUser user = new TUser();
user.setIsdel(false);
user.setRemark("testInsert");
user.setUsername("Zhang San");
dao.save(user);
System.out.println("Added primary key:" + user.getId());
}
@Test
public void testGet() {
TUser user = dao.get(3);
System.out.println(user);
}
@Test
public void testUpdate() {
TUser user = dao.get(3);
user.setUsername("Li Si");
user.setIsdel(true);
int i = dao.update(user);
System.out.println("testUpdate --> " + i);
}
@Test
public void testDel() {
TUser user = new TUser();
user.setId(3);
int i = dao.del(user);
System.out.println("del --> " + i);
}
}
Then run the unit test, running successfully indicates that the project has been built.
The final project structure chart:
Using easymybatis in spring MVC structure project
- Add easymybatis.jar to maven
<!-- easymybatis --> <dependency> <groupId>net.oschina.durcframework</groupId> <artifactId>easymybatis</artifactId> <version>1.7.2</version> </dependency>
- Replace SqlSessionFactoryBean with SqlSessionFactoryBeanExt and add a basePackage attribute
<! - Replace org. mybatis. spring. SqlSessionFactoryBean - >. <bean id="sqlSessionFactory" class="net.oschina.durcframework.easymybatis.ext.SqlSessionFactoryBeanExt"> <property name="dataSource" ref="dataSource" /> ... <! - Here are the additional attributes - >. <! - Dao's package name is the same as Mapper Scanner Configurer's base package Multiple uses; isolation. --> <property name="basePackage" value="com.example.demo2.dao" /> </bean>
Finally, add entity classes and dao to run.