使用IDEA建立一个SpringBoot项目。
1、POM文件:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>MyBatisBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>MyBatisBoot</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
2、建立实体类SysUser:
1 package com.yas.entity; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 import lombok.ToString; 7 8 import java.util.Date; 9 10 @Data 11 @NoArgsConstructor 12 @AllArgsConstructor 13 @ToString 14 public class SysUser { 15 private Integer id; 16 private String account; 17 private String username; 18 private String pwd; 19 private Date gmt_create; 20 private Date gmt_modified; 21 }
3、增删改查的UserDAO,(使用注解方式):
1 package com.yas.dao; 2 3 import com.yas.entity.SysUser; 4 import org.apache.ibatis.annotations.*; 5 6 import java.util.List; 7 import java.util.Map; 8 9 @Mapper 10 public interface UserDAO { 11 @Select("SELECT * FROM sys_user WHERE id = #{id}") 12 SysUser queryUserById(Integer id); 13 14 //使用@Param注解,标注参数 15 @Select("SELECT * FROM sys_user WHERE id = #{id} and username = #{username}") 16 SysUser queryUserByIdAndUsername(@Param("id") Integer id,@Param("username") String username); 17 18 //使用Map封装参数 19 @Select("SELECT * FROM sys_user WHERE id = #{id} and username = #{username}") 20 SysUser queryUserByMap(Map map); 21 22 @Select("SELECT * FROM sys_user WHERE id = #{id} and username = #{username}") 23 SysUser queryUserByEntity(SysUser user); 24 25 @Select("SELECT * FROM sys_user WHERE username like concat('%',#{username},'%')") 26 List<SysUser> queryUserByUsername(@Param("username") String username); 27 28 @Delete("DELETE FROM sys_user WHERE id = #{id}") 29 Integer deleteUser(@Param("id") Integer id); 30 31 @Update("UPDATE sys_user SET username = #{username} WHERE id = #{id}") 32 Integer updateUser(SysUser user); 33 34 @Insert("INSERT INTO sys_user VALUES(#{id},#{account},#{username},#{pwd},#{gmt_create},#{gmt_modified})") 35 @Options(useGeneratedKeys = true,keyProperty = "id") 36 Integer insertUser(SysUser user); 37 }
4、在resources目录下的,application.properties文件中添加配置信息:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/lending_management?characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=root #开启mybatis的数据库日志 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
5、测试类:
1 package com.yas; 2 3 import com.yas.dao.UserDAO; 4 import com.yas.entity.SysUser; 5 import org.junit.jupiter.api.Test; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 9 import java.util.*; 10 11 @SpringBootTest 12 class MyBatisBootApplicationTests { 13 @Autowired 14 UserDAO userDAO; 15 16 @Test 17 void queryUserById() { 18 // SysUser user1 = userDAO.queryUserById(1); 19 // System.out.println(user1); 20 21 // SysUser user2 = userDAO.queryUserByIdAndUsername(2,"刘翔"); 22 // System.out.println(user2); 23 24 // Map<String,String> map = new HashMap<>(); 25 // map.put("id","1"); 26 // map.put("username","管理员"); 27 // SysUser user3 = userDAO.queryUserByMap(map); 28 // System.out.println(user3); 29 30 // SysUser user = new SysUser(); 31 // user.setId(1); 32 // user.setUsername("管理员"); 33 // SysUser user4 = userDAO.queryUserByEntity(user); 34 // System.out.println(user4); 35 36 // List<SysUser> users = userDAO.queryUserByUsername("C"); 37 // for (SysUser user : users) { 38 // System.out.println(user); 39 // } 40 41 // userDAO.deleteUser(83); 42 43 SysUser user = new SysUser(5,"manager","李白","000000",new Date(),new Date()); 44 userDAO.insertUser(user); 45 } 46 }
补充,如果使用注解与xml配置文件混合开发的方式,可以在配置文件中添加如下地址:
mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
在resources目录下建立一个mybatis文件夹,然后放置核心配置文件mybatis-config.xml,就可以读取xml文件中的配置了。
在resources/mapper目录下建立以.xml为结尾的映射文件,可以使用xml配置的方式,编写增删改查的语句。
但注意,对于同一个接口方法,使用注解或使用xml只能同时使用其中一种,否则就会报错。