springBoot(11):集成Mybatis
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,一、添加依赖 org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 mysql mysql
千家信息网最后更新 2025年02月02日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安全错误
数据库的锁怎样保障安全
智能骑行台软件开发论文
万客互联网科技有限公司
幼儿拼图软件开发
公安网络安全应急工作
网络技术学院风穿搭
常见的数据库服务器
体验软件开发
软件开发是高新技术吗
网络安全会带的问题
论网络技术在农业传播
互联网行业网络安全审查
宜昌网络安全审计
ip地址数据库是干啥的
网络安全虚拟仿真厂家
java 多数据库连接框架
密云软件开发者
服务器 修改密码
oracle数据库分区
一次网络安全应急演练
软件开发技术方面主要研究
襄阳本地软件开发小公司
高级数据库管理技术阶段特点
数据库理论基础学生课堂笔记
显示器和服务器连接不了
lol手游转服务器多少钱
精通mysql 数据库
实体类 数据库 dao
网络安全工作责任标准
layui数据库修改传参
网络安全工程师工作地分析