您好,登錄后才能下訂單哦!
一、添加依賴
<!--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、配置數(shù)據(jù)源
##################################mysql數(shù)據(jù)源配置################################## 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); /**根據(jù)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); } }
運(yùn)行insert方法:
運(yùn)行select方法:
三、基于mybatis的xml集成
3.1、配置
##################################mysql數(shù)據(jù)源配置################################## 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); /**根據(jù)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); } }
運(yùn)行insert方法:
運(yùn)行select方法:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。