springBoot(11):集成Mybatis
发表于:2025-11-19 作者:千家信息网编辑
千家信息网最后更新 2025年11月19日,一、添加依赖 org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 mysql mysql
千家信息网最后更新 2025年11月19日springBoot(11):集成Mybatis
一、添加依赖
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-8spring.datasource.username=rootspring.datasource.password=123456spring.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 */@Mapperpublic 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)@SpringBootTestpublic 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-8spring.datasource.username=rootspring.datasource.password=123456spring.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 */@Mapperpublic 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)@SpringBootTestpublic 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方法:

用户
方法
测试
运行
配置
我爱
数据
数据源
大金
代码
实体
查询
别名
注解
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
虚拟服务器云服务器售价
移动终端软件开发就业前景
护苗.网络安全课有感
互联网法院用科技打通了现实
富士康服务器bios查看网卡
从数据库系统的结构单用户
2021 大富网络技术
天水樱桃网络技术有限公司骗局
dayz服务器下雨
武汉腾晟网络技术有限公司
智慧教室系统软件开发
为软件开发能力自评估
数据库表空间大小查看及解析
苹果手机激活出现服务器出错
最新数据库
杨浦区上门软件开发价格表格
湖南吉栈网络技术有限公司
德职计算机网络技术工资
服务器地址无法登录
拷贝mysql数据库文件
监听bmob数据库
网络安全法4大制度
osi网络安全体系特点
软件开发人工成本
孤岛惊魂6钻井平台服务器机房
网页前端连接数据库吗
软件开发过程记录
数据库重要知识点博客
服务器是一个月收费一次
数据采集数据库备份文件