spring boot 和 mybatis集成
1、pom.xml 添加mybatis和mysql依赖
<!-- 添加 MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency> <!-- 添加 MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
2、application.properties 里面配置数据库信息
#数据库数据源配置
spring.datasource.url=jdbc:mysql://localhost/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3、配置mybatis,有两种方式,一种用注解,一种用xml,一般习惯用XML。
- 用注解
@Mapper
public interface UserMapper {
/**
* 查询所有用户
* @return
*/
@Select("select id,name,age FROM tb_tic_user")
List<UserDo> selectAll();
}
public class UserDo implements Serializable{
private static final long serialVersionUID = -7488908967791971359L;
private int Id;
private String name;
private int age;
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "UserDo{" +
"Id=" + Id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Controller
public class IndexController { @Autowired
UserMapper userMapper; @RequestMapping("/selectAll")
public @ResponseBody List<UserDo> selectAll(){
return userMapper.selectAll();
} }
启动不报错,用注解配置成功,主要是在Mapper类上增加@Mapper注解,sql语句用注解放在方法上面。配置简单,但是会有重复代码,不推荐。
- 用xml配置,增加配置类用来扫描mapper接口
application.properties 中增加mybatis配置
#mybatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.domain
@Configuration
@MapperScan(value = "com.example.demo.mapper")
public class MybatisConfig {
}
package com.example.demo.mapper;
import com.example.demo.domain.Test;
public interface TestMapper {
int deleteByPrimaryKey(Integer id);
int insert(Test record);
int insertSelective(Test record);
Test selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Test record);
int updateByPrimaryKey(Test record);
}
<?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.TestMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.domain.Test" >
<id column="ID" property="id" jdbcType="INTEGER" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
ID, NAME
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_tic_test
where ID = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_tic_test
where ID = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.demo.domain.Test" >
insert into tb_tic_test (ID, NAME)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.example.demo.domain.Test" >
insert into tb_tic_test
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="name != null" >
NAME,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.domain.Test" >
update tb_tic_test
<set >
<if test="name != null" >
NAME = #{name,jdbcType=VARCHAR},
</if>
</set>
where ID = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.domain.Test" >
update tb_tic_test
set NAME = #{name,jdbcType=VARCHAR}
where ID = #{id,jdbcType=INTEGER}
</update>
</mapper>
@Controller
public class IndexController { @Autowired
TestMapper testMapper; @RequestMapping("/test")
public @ResponseBody Test test(){
return testMapper.selectByPrimaryKey(1);
}
}
习惯用xml,这样方便把基础方法提出来
spring boot 和 mybatis集成的更多相关文章
- spring boot和mybatis集成分页插件
MyBatis提供了拦截器接口,我们可以实现自己的拦截器,将其作为一个plugin装入到SqlSessionFactory中. 首先要说的是,Spring在依赖注入bean的时候,会把所有实现MyBa ...
- spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页
controller: /** * 分页查询用户 * @param request * @param response * @return * @throws Exception */ @ ...
- Spring Boot 数据访问集成 MyBatis 与事物配置
对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...
- 6、Spring Boot 2.x 集成 MyBatis
1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...
- Spring boot 与mybatis 多数据源问题
https://www.cnblogs.com/ityouknow/p/6102399.html Spring Boot 集成Mybatis实现多数据源 https://blog.csdn.net/m ...
- 7、Spring Boot 2.x 集成 Redis
1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
随机推荐
- 弹出ifream
top.$.jBox("iframe:"+'${ctx}/synopsis/hmlwxSynopsis/addItem', {title: "添加作品",w ...
- oracle函数nvl,nvl2的区别,nullif函数,coalesce函数
在oracle中用nvl和nvl2函数来解决为空的情况,例如,如果奖金为空,则为它指定一个数.也就是nvl(奖金字段,指定的奖金),但是两个的类型要一致. 1)nvl()函数 SQL> sele ...
- hadoop面试复习笔记(1)
0.Mappereduce采用的是Master/Slaves模型 1.Hadoop是一个开源软件框架,支持支持大数据集的存储和处理.Apache Hadoop是存储和处理大数据的解决方案你是因为: ( ...
- matlab 中figure的图像 抗锯齿
linehandle = plot(xxxxxx); set( linehandle, 'linesmoothing', 'on' );
- 跳跃表-原理及Java实现
跳跃表-原理及Java实现 引言: 上周现场面试阿里巴巴研发工程师终面,被问到如何让链表的元素查询接近线性时间.笔者苦思良久,缴械投降.面试官告知回去可以看一下跳跃表,遂出此文. 跳跃表的引入 我们知 ...
- 查看系统的DPI
#include <Windows.h> #include <iostream> int main() { SetProcessDpiAwarenessContext(DPI_ ...
- MySQL数据表
创建数据表 CREATE TABLE IF NOT EXISTS ([列名column][类型type][约束可选]) 查看数据表结构 DESC <表名> 修改数据表结构 ALTER ...
- 轻松学习JVM——垃圾回收器
原文链接:https://www.cnblogs.com/leefreeman/p/7402695.html 上一篇我们介绍了常见的垃圾回收算法,不同的算法各有各的优缺点,在JVM中并不是单纯的使用某 ...
- NOIP2015普及组总结
NOIP2015普及组总结 这次考试总体感觉不错,不过觉得时间有点紧,在最后30分钟才打完. 第一题(金币coin):大大的W!爆搜O(N),一分钟打完: 第二题(扫雷游戏mine):同上: 第三题( ...
- Kafka(华为FusionInsight )操作命令
华为大数据kafka操作web界面创建角色.用户.用户管理角色进入服务器环境,进入客户端目录/opt/hadoopclient,导入环境变量source bigdata_env.切换用户kinit k ...