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 ...
随机推荐
- sql server ABS函数和PI函数
--ABS(x)返回x的绝对值 --PI()返回圆周率的值
- h5与app混合开发,jsbridge
https://juejin.im/post/5bda6f276fb9a0226d18931f https://juejin.im/post/5abca877f265da238155b6bc http ...
- 机器学习-聚类(clustering)算法:K-means算法
1. 归类: 聚类(clustering):属于非监督学习(unsupervised learning) 无类别标记(class label) 2. 举例: 3. Kmeans算法 3.1 clust ...
- stars-one的原创工具——星之小说下载器(JavaFx应用 )
星之小说下载器Kotlin版 基于星之小说下载器Java版重构的Kotlin版本 github地址 使用说明 确保电脑有jdk8+以上的环境,双击即可运行(win10系统),win7则需要输入命令ja ...
- uboot initf_dm函数分析
initf_dm, static int initf_dm(void){#if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN) / ...
- python接口自动化六(参数化也就是把之前敲过的代码封装成方法)
前言 前面一篇实现了参数的关联,那种只是记流水账的完成功能,不便于维护,也没什么可读性,接下来这篇可以把每一个动作写成一个函数,这样更方便了. 参数化的思维只需记住一点:不要写死 (由于博客园登录机制 ...
- JAVA中的23种设计模式
http://blog.csdn.net/chmask/article/details/2631485 http://www.cnblogs.com/hnrainll/archive/2011/12/ ...
- electron-vue 更新 使用electron-update的版本
electron-vue使用的electron版本比较老,用最新的electron-update会报错 我用这个版本成功 "electron-updater": "^3. ...
- 【leetcode】638. Shopping Offers
题目如下: In LeetCode Store, there are some kinds of items to sell. Each item has a price. However, ther ...
- Fiddler的工作原理与主菜单介绍(一)
1.简介: 官网:https://www.telerik.com/fiddler Fiddler是比较好用的web代理调试工具之一,它能记录并检查所有客户端与服务端的HTTP/HTTPS请求,能够设置 ...