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

SpringBoot-mybatis

时间:2019-01-08 15:18:27      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:ann   connect   source   from   1.3   1.0   path   model   ott   

springboot集成mybatis有两种方式:注解方式和xml配置方式。

项目的相关配置:

1、添加依赖

<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>
</dependency>

 2、application.properties 配置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=***

 

一、注解方式

UserMapper:

@Mapper // 可以在Applicationz @MapperScan()
public interface UserMapper {

    @Select("SELECT * FROM tb_user")
    @Results({
            @Result(property = "userName", column = "user_name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userPassword", column = "user_password", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userSex", column = "user_sex", jdbcType = JdbcType.CHAR),
            @Result(property = "userBirthday", column = "user_birthday", jdbcType = JdbcType.DATE)
    })
    List<User> getAllUser();

    @Select("SELECT * FROM tb_user WHERE uid = #{uid}")
    @Results({
            @Result(property = "userName", column = "user_name", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userPassword", column = "user_password", jdbcType = JdbcType.VARCHAR),
            @Result(property = "userSex", column = "user_sex", jdbcType = JdbcType.CHAR),
            @Result(property = "userBirthday", column = "user_birthday", jdbcType = JdbcType.DATE)
    })
    User getUser(int uid);

    @Insert("INSERT INTO tb_user(user_name,user_password,user_sex,user_birthday) VALUES(#{userName}, #{userPassword}, #{userSex}, #{userBirthday})")
    int insertUser(User user);

    @Update("UPDATE tb_user SET user_name = #{userName} WHERE uid = #{uid}")
    int updateUser(User user);

    @Delete("DELETE FROM tb_user WHERE uid = #{uid}")
    int deleteUser(User user);
}

 测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AnnotionTest {

    @Autowired
    private UserMapper mapper;

    @Test
    public void getAllUser(){
        List<User> users = mapper.getAllUser();
        System.out.println(users);
    }

    @Test
    public void getUser(){
        User user = mapper.getUser(3);
        System.out.println(user);
    }

    @Test
    public void insertUser(){
        User user = new User();
        user.setUserName("王杰");
        user.setUserPassword("123456546313154546");
        user.setUserSex(‘2‘);
        user.setUserBirthday(new Date());

        int result = mapper.insertUser(user);
        System.out.println(result);
    }

    @Test
    public void updateUser(){
        User user = new User();
        user.setUid(3);
        user.setUserName("刘冷");

        int result = mapper.updateUser(user);
        System.out.println(result);
    }

    @Test
    public void deleteUser(){
        User user = new User();
        user.setUid(3);

        int result = mapper.deleteUser(user);
        System.out.println(result);
    }

}

 

二、xml配置方式

application.properties 中添加配置:

mybatis.mapper-locations=classpath:/mybatis/mapper/*.xml //映射文件的位置

 Application.java中添加注解:

@MapperScan("com.swp.mybatisxml.mapper") // 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">
<mapper namespace="com.swp.mybatisxml.mapper.UserMapper">

    <resultMap id="UserMap" type="com.swp.mybatisxml.model.User">
        <id property="uid" column="uid" jdbcType="INTEGER"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userPassword" column="user_password" jdbcType="VARCHAR"/>
        <result property="userSex" column="user_sex" jdbcType="CHAR"/>
        <result property="userBirthday" column="user_birthday" jdbcType="DATE"/>
    </resultMap>

    <sql id="Base_Column">
        uid, user_name, user_password, user_sex, user_birthday
    </sql>

    <select id="getAllUser" resultMap="UserMap">
        SELECT
        <include refid="Base_Column"/>
        FROM tb_user
    </select>

    <select id="getUser" resultMap="UserMap" parameterType="java.lang.Integer">
        SELECT
        <include refid="Base_Column"/>
        FROM tb_user WHERE uid = #{uid}
    </select>

    <insert id="insertUser" parameterType="com.swp.mybatisxml.model.User">
        INSERT INTO tb_user(user_name,user_password,user_sex,user_birthday) VALUES(#{userName}, #{userPassword}, #{userSex}, #{userBirthday})
    </insert>

    <update id="updateUser" parameterType="com.swp.mybatisxml.model.User">
        UPDATE tb_user SET
        <if test="userName != null">user_name = #{userName}</if>
        WHERE uid = #{uid}
    </update>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        DELETE FROM tb_user WHERE uid = #{uid}
    </delete>

</mapper>

 

SpringBoot-mybatis

标签:ann   connect   source   from   1.3   1.0   path   model   ott   

原文地址:https://www.cnblogs.com/king-peng/p/10238324.html

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