springBoot(11):集成Mybatis
一、添加依赖
安丘ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 MySQL mysql-connector-java runtime
二、基于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
id, name, create_time insert into user (name, create_time) values (#{name,jdbcType=VARCHAR},#{createTime,jdbcType=TIMESTAMP})
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方法:
本文题目:springBoot(11):集成Mybatis
本文URL:http://myzitong.com/article/goepcj.html