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集成的更多相关文章

  1. spring boot和mybatis集成分页插件

    MyBatis提供了拦截器接口,我们可以实现自己的拦截器,将其作为一个plugin装入到SqlSessionFactory中. 首先要说的是,Spring在依赖注入bean的时候,会把所有实现MyBa ...

  2. spring boot +Thymeleaf+mybatis 集成通用PageHelper,做分页

    controller: /**  * 分页查询用户  * @param request  * @param response  * @return  * @throws Exception  */ @ ...

  3. Spring Boot 数据访问集成 MyBatis 与事物配置

    对于软件系统而言,持久化数据到数据库是至关重要的一部分.在 Java 领域,有很多的实现了数据持久化层的工具和框架(ORM).ORM 框架的本质是简化编程中操作数据库的繁琐性,比如可以根据对象生成 S ...

  4. 6、Spring Boot 2.x 集成 MyBatis

    1.6 Spring Boot 2.x 集成 MyBatis 简介 详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射. 完整源码: 1.6.1 创建 spring-bo ...

  5. Spring boot 与mybatis 多数据源问题

    https://www.cnblogs.com/ityouknow/p/6102399.html Spring Boot 集成Mybatis实现多数据源 https://blog.csdn.net/m ...

  6. 7、Spring Boot 2.x 集成 Redis

    1.7 Spring Boot 2.x 集成 Redis 简介 继续上篇的MyBatis操作,详细介绍在Spring Boot中使用RedisCacheManager作为缓存管理器,集成业务于一体. ...

  7. spring boot rest 接口集成 spring security(2) - JWT配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  8. spring boot rest 接口集成 spring security(1) - 最简配置

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  9. Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...

随机推荐

  1. 两张超级大表join优化

    一个简单的两表关联,SQL跑了差不多一天一夜,这两个表都非常巨大,每个表都有几十个G,数据量每个表有20多亿,表的字段也特别多. 相信大家也知道SQL慢在哪里了,单个进程的PGA 是绝对放不下几十个G ...

  2. C语言数据类型关键字

    最初 K&R 给出的关键字 C90 标准添加的关键字 C99 标准添加的关键字 int signed _Bool long void _Complex short   _Imaginary u ...

  3. Jquery复习(五)之append()、appendTo()、prepend()、prependTo()、after()、before()易忘点

    添加元素的方法 append().appendTo().prepend().prependTo().after().before() 通过 append() .appendTo().prepend() ...

  4. redis 模拟jedis 操作string类型数据

    一.思路分析 redis数据传输遵循resp协议,只需要按照resp协议并通过socket传递数据到redis服务器即可 resp数据格式: 二.具体实现 package com.ahd.jedis; ...

  5. nodejs fs copy本地文件src dst

    1. // fs.writeFileSync(pathNewFile, fs.readFileSync(fileName)); 2.   fs.createReadStream(fileName).p ...

  6. python_实现选课系统

    校园管理系统 角色: 学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开 3 ...

  7. (转)MyBatis 一、二级缓存和自定义缓存

    1.一级缓存 MyBatis 默认开启了一级缓存,一级缓存是在SqlSession 层面进行缓存的.即,同一个SqlSession ,多次调用同一个Mapper和同一个方法的同一个参数, 只会进行一次 ...

  8. 解决Kloxo出现Could not open database connection问题

    当我们在使用或者运行kloxo面板的时候,可能会出现类似"Could not open database connection"错误提示,对于新手朋友来说肯定本身安装面板管理VPS ...

  9. [易学易懂系列|rustlang语言|零基础|快速入门|(13)|Generics泛型]

    [易学易懂系列|rustlang语言|零基础|快速入门|(13)] 有意思的基础知识 Generics泛型 我们今天来看看泛型. 什么是泛型? 我们来看看这样的情景: 我们要写一个函数,这个函数可以处 ...

  10. linux资源管理命令之-----vmstat

    一.作用及语法: 命令用来显示Linux系统虚拟内存状态,也可以报告关于进程.内存.I/O等系统整体运行状态. vmstat [options] [delay [count]] 二.vmstat各字段 ...