码迷,mamicode.com
首页 > 编程语言 > 详细

【SpringBoot__Mybatis】整合MyBatis 配置文件版2

时间:2021-03-06 14:34:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:add   amp   into   pack   tis   cal   test   ESS   文件的   

1、引用pom文件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <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.1.3</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>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

2、yml

spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis.type-aliases-package=com.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

3、UserMapper

@Mapper
@Repository
public interface UserMapper {
    public static final int age = 111;

    List<User> queryUserList();

    User getUserById(int id);

    int addUser(User user);

    int update(User user);

    int delete(int id);
}

4、mybatis/mapper/UserMapper.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">

<!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="com.mapper.UserMapper">
    <resultMap type="User" id="userResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="gender" column="gender"/>
    </resultMap>

    <resultMap id="user" type="user">
        <id column="id" property="id"/>
        <result column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="birthday" property="birthday" javaType="java.sql.Date"/>
    </resultMap>

    <!-- 增加一条数据 -->
    <insert id="addUser" parameterType="User">
        INSERT INTO user
        VALUES (null, #{name}, #{age}, #{gender})
    </insert>

    <!-- 删除一条数据 -->
    <delete id="delete" parameterType="int">
        DELETE
        FROM user
        WHERE id = #{id}
    </delete>

    <!-- 更新一条数据 -->
    <update id="update" parameterType="User">
        UPDATE user
        SET name   = #{name},
            age    = #{age},
            gender = #{gender}
        WHERE id = #{id}
    </update>

    <!-- 查询一条数据 -->
    <select id="getUserById" resultMap="userResult" parameterType="int">
        SELECT *
        FROM user
        WHERE id = #{id}
    </select>


    <!-- 查询从start位置开始的count条数据-->
    <select id="queryUserList" resultMap="userResult">
        SELECT *
        FROM user
    </select>

</mapper>

5、UserController

@RestController
public class UserController {
    @Autowired
    private UserMapper mapper;

    @GetMapping("/queryUserList")
    public List<User> queryUserList() {
        List<User> users = mapper.queryUserList();
        for (User u1 : users) {
            System.out.println(u1);
        }
        return users;
    }
}

【SpringBoot__Mybatis】整合MyBatis 配置文件版2

标签:add   amp   into   pack   tis   cal   test   ESS   文件的   

原文地址:https://www.cnblogs.com/kikyoqiang/p/14487717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!