一、添加依赖
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
二、基于mybais注解的集成
2.1、配置数据源
##################################mysql数据源配置################################## spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.2、java代码
User.java
package com.example.demo.pojo; import java.io.Serializable; import java.util.Date; /** * 用户实体类 * @Author: 我爱大金子 * @Description: 用户实体类 * @Date: Created in 14:25 2017/6/18 */ public class User implements Serializable { private Integer id; private String name; private Date createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", createTime=" + createTime + '}'; } }
UUserMapper.java
package com.example.demo.mapper; import com.example.demo.pojo.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.type.JdbcType; /** * 用户Mapper * @Author: 我爱大金子 * @Description: 用户Mapper * @Date: Created in 14:28 2017/6/18 */ @Mapper public interface UserMapper { /**添加用户*/ @Insert(value = "insert into user (name,create_time) values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})") int insert(User record); /**根据id查询用户*/ @Select(value = "select id, name, create_time from user where id = #{id,jdbcType=INTEGER}") @Results(value = { @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP) }) User selectByPrimaryKey(Integer id); }
2.3、测试
package com.example.demo; import com.example.demo.mapper.UserMapper; import com.example.demo.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootDemo27ApplicationTests { @Autowired private UserMapper mapper; @Test public void insert() { User user = new User(); user.setName("测试"); user.setCreateTime(new Date()); int result = mapper.insert(user); System.out.println(result); } @Test public void select() { User result = mapper.selectByPrimaryKey(1); System.out.println(result); } }
运行insert方法:
运行select方法:
三、基于mybatis的xml集成
3.1、配置
##################################mysql数据源配置################################## spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ##################################mybatis基于xml集成################################## #扫包 mybatis.mapper-locations: classpath:mybatis/*.xml #别名 #mybatis.type-aliases-package: com.example.demo.pojo
3.2、java代码
UUserMapper2.java
package com.example.demo.mapper; import com.example.demo.pojo.User; import org.apache.ibatis.annotations.*; import org.apache.ibatis.type.JdbcType; /** * 用户Mapper * @Author: 我爱大金子 * @Description: 用户Mapper * @Date: Created in 14:28 2017/6/18 */ @Mapper public interface UserMapper2 { /**添加用户*/ int insert(User record); /**根据id查询用户*/ User selectByPrimaryKey(Integer id); }
UserMapper2.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.example.demo.mapper.UserMapper2" > <resultMap id="BaseResultMap" type="com.example.demo.pojo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="create_time" property="createTime" jdbcType="TIMESTAMP" /> </resultMap> <sql id="Base_Column_List" > id, name, create_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <insert id="insert" parameterType="com.example.demo.pojo.User" > insert into user (name, create_time) values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP}) </insert> </mapper>
3.3、测试
package com.example.demo; import com.example.demo.mapper.UserMapper; import com.example.demo.mapper.UserMapper2; import com.example.demo.pojo.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootDemo27ApplicationTests { @Autowired private UserMapper2 mapper2; @Test public void insert() { User user = new User(); user.setName("测试2"); user.setCreateTime(new Date()); int result = mapper2.insert(user); System.out.println(result); } @Test public void select() { User result = mapper2.selectByPrimaryKey(2); System.out.println(result); } }
运行insert方法:
运行select方法:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。