Spring Boot整合Mybatis
1、POM.xml依赖文件
<?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 http://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.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.smart</groupId> <artifactId>boot_mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>boot_mybatis</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>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> </plugin> </plugins> </build> </project>
2、创建实体类
package com.smart.boot_mybatis.domain; public class User { private Long id; private String name; private int age; private String address; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
3、映射接口
package com.smart.boot_mybatis.mapper; import com.smart.boot_mybatis.domain.User; import java.util.List; public interface UserMapper { int addUser(User user); int deleteUserById(Long id); int updateUserById(User user); User queryUserById(Long id); List<User> queryUserList(); }
4、对应的xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.smart.boot_mybatis.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.smart.boot_mybatis.domain.User"> <result column="t_id" property="id"/> <result column="t_name" property="name"/> <result column="t_age" property="age"/> <result column="t_address" property="address"/> </resultMap> <sql id="Base_Column_List"> t_id,t_name,t_age,t_address </sql> <insert id="addUser" parameterType="com.smart.boot_mybatis.domain.User"> insert into t_user(t_id,t_name,t_age,t_address) values (#{id},#{name},#{age},#{address}); </insert> <delete id="deleteUserById" parameterType="java.lang.Long"> delete from t_user where t_id=#{id} </delete> <update id="updateUserById" parameterType="com.smart.boot_mybatis.domain.User"> update t_user set <trim suffixOverrides=","> <if test="id !=null and id !=''"> t_id=#{id}, </if> <if test="name != null and name != ''"> t_name=#{name}, </if> <if test="age != null and age != ''"> t_age=#{age}, </if> <if test="address != null and address != ''"> t_address=#{address}, </if> where t_id=#{id}; </trim> </update> <select id="queryUserById" resultMap="BaseResultMap" parameterType="java.lang.Long"> select <include refid="Base_Column_List"/> from t_user where t_id=#{id} </select> <select id="queryUserList" resultMap="BaseResultMap"> select <include refid="Base_Column_List"/> from t_user </select> </mapper>
5、application.yml文件
spring: datasource: url: jdbc:mysql://127.0.0.1:3306/world?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC driverClassName: com.mysql.cj.jdbc.Driver username: admin password: admin mybatis: mapper-locations: classpath*:mybatis/mapper/*.xml logging: level: com: smart: boot_mybatis: mapper: debug
6、访问控制器
package com.smart.boot_mybatis.controller; import com.smart.boot_mybatis.domain.User; import com.smart.boot_mybatis.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value="/user") public class UserController { @Autowired UserMapper userMapper; @RequestMapping("/{userId}") public User getUserById(@PathVariable(value="userId") Long id){ return userMapper.queryUserById(id); } }
7、启动器
package com.smart.boot_mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.smart.boot_mybatis.mapper") public class BootMybatisApplication { public static void main(String[] args) { SpringApplication.run(BootMybatisApplication.class, args); } }
立志如山 静心求实
浙公网安备 33010602011771号