Spring 中应用Mybatis,超详细
目录
如果您对Spring基础框架不是很了解请先看Spring 配置详解。
一、Mybatis 的作用
1、MyBatis(前身为iBatis)是一个开源的Java持久层框架,它主要用于与数据库交互,帮助开发者更轻松地进行数据库操作。
2、MyBatis 提供了一种简单而强大的方式来执行数据库操作,包括查询、插入、更新和删除。开发者可以使用XML配置文件或者注解来定义SQL语句,并且可以将SQL语句的参数映射到Java对象,以便进行数据库操作。
3、MyBatis 提供了缓存机制,可以帮助提高查询性能。你可以配置缓存来存储查询结果,以便在后续查询中重用结果,从而减少数据库访问次数。
4、MyBatis 支持事务管理,可以确保数据库操作的原子性和一致性。你可以通过配置或编程方式管理事务,以满足应用程序的需求。
二、使用步骤
1、创建数据库和表
2、添加maven依赖
3、编写实体类
4、创建XXXMapper接口
5、配置XXXMapper.xml
6、配置数据库连接信息 JDBC
7、在Spring中配置Mybatis
8、在Spring中调用Mybatis查询数据
项目整体结构如下图:

三、创建数据库和表
请自行安装数据库并创建表。我的数据库名为yiqifu,表为名u_user,字段如下图。
CREATE TABLE `U_USER` (
`id` int(255) NULL ,
`nickname` varchar(255) NULL
)
;
四、添加Maven依赖包
分别添加mysql-connector-java(用于连接mysql数据库)、mybatis(mybatis核心库)、mybatis-spring(在spring中使用mybatis的库)的maven依赖包。
org.springframework
spring-context
5.3.29
mysql
mysql-connector-java
8.0.23
org.mybatis
mybatis
3.5.11
org.mybatis
mybatis-spring
2.0.7
org.springframework
spring-jdbc
5.3.29
com.alibaba
druid
1.2.8
test
五、编写实体类
根据数据表的字段创建一个实体类,各字段可以不跟数据库一致。对应关系可以在Mapper.xml中配置
package top.yiqifu.study.p061_mybatis;
public class UserEntity {
public int id;
public String name;
public UserEntity(){
}
public UserEntity(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
}
六、定义UserMapper接口
UserMapper接口的作用是告诉mybatis您要对数据库执行那些操作。具体实现类由Spring AOP完成。其中执行的SQL语句可以通过Mappser.xml配置,也可以在这里使用注解配置。我这里仅定义了几个简单的增删改查。
package top.yiqifu.study.p061_mybatis;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
void insert(UserEntity user);
void deleteById(Integer id);
void update(UserEntity user);
List findAll();
@Select("select * from u_user where id=#{id}")
@Results({
@Result(property = "name", column = "nickname"), // 指定属性名和列名的映射关系
})
UserEntity findById(Integer id);
}
七、配置UserMapper.xml
UserMapper.xml的作用是告诉mybatis您在UserMapper接口定义的方法具体使用什么样的SQL及其他约束。我这里简单配置了UserMapper接口中每个方法。
注:其实也可以直接在UserMapper接口中使用注解定义(请看findById方法),使用XML定义是为了解耦。
insert into u_user(nickname)
values
(
#{name}
)
delete from u_user where id=#{id}
update u_user set nickname=#{name} where id=#{id}
八、配置数据库连接信息 JDBC
配置jdbc.properties(放在resources目录下)的作用时指定数据连接信息。
database.driver=com.mysql.cj.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/yiqifu?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
database.username=root
database.password=123456xxoo
九、在Spring中配置Mybatis
配置applicationContext-mybatis.xml(放在resources目录下)的作用将前的内容整合在起。包括mybatis的JDBC环境,接口映射和接口配置。
十、在Spring中调用Mybatis查询数据
以下是调用示例。
package top.yiqifu.study.p061_mybatis;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.yiqifu.study.p051_proxy.Test011_StaticProxyDog;
import top.yiqifu.study.p051_proxy.Test041_Animal;
import top.yiqifu.study.p051_proxy.Test042_Dog;
public class Test001_Mybatis
{
// 静态代理
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
UserMapper mapper = context.getBean("userMapper", UserMapper.class);
for(String beanName : context.getBeanDefinitionNames()){
System.out.println(beanName);
}
UserEntity user1 = new UserEntity();
UserEntity user2 ;
//添加
user1.setName("test");
mapper.insert(user1);
int userId = 7;
user2 = mapper.findById(userId);
System.out.println(user2);
//修改
user1.setId(userId);
user1.setName("new-test");
mapper.update(user1);
//查询
user2 = mapper.findById(userId);
System.out.println(user2);
//删除
//mapper.deleteById(userId);
user2 = mapper.findById(userId);
System.out.println(user2);
}
}

浙公网安备 33010602011771号