SSM之MyBatis 01 —— 第一个MyBatis程序、增删改查(模糊查询)
SSM之MyBatis 02 —— 配置文件说明、日志工厂、分页(Limit和RowBounds)
SSM之MyBatis 03 —— 使用注解开发、Lombok、多对一&一对多处理
SSM之MyBatis 04 —— 动态SQL、缓存Cache
如何获得MyBatis:
maven仓库
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
GitHub:https://github.com/mybatis/mybatis-3/releases
官方手册:https://mybatis.org/mybatis-3/zh/index.html
为什么需要持久化?最主要是有一些对象需要长时间保存。
数据持久化:
持久层就是完成持久化的代码块,持久化是一个动作,而持久层是一种概念。
最重要的是使用MyBatis的人很多!!
第一个MyBatis程序,在数据库新建一个用户表,通过MyBatis进行查询。
步骤:搭建环境、导入MyBatis、写代码、测试
目录结构
搭建数据库
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL,
`name` VARCHAR(20) DEFAULT NULL,
`password` VARCHAR(20) DEFAULT NULL,
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`password`) VALUES
(1, '小白', '123456'),
(2, '小红', '123456'),
(3, '小黑', '123456');
新建项目
1、创建普通maven项目
2、删除src目录
3、导入maven依赖
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
1、直接创建一个普通maven模块
2、按照手册说的,写好MyBatis配置文件,放在resources目录下
mybatis-config.xml(根据自己数据库写参数)
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
dataSource>
environment>
environments>
configuration>
3、编写MyBatis工具类
为了方便后面使用MyBatis,编写一个工具类是有必要的。
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
//引用前面的MyBatis配置文件,该文件放在resources目录下
String resource = "mybatis-config.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//后续对数据库的操作都用Sqlsession类代替
public SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
1、实体类
根据数据库的user表创建对应的实体类
package pojo;
public class User {
private int id;
private String name;
private String password;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public User() {
}
public User(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
}
2、Dao接口(对比之前的smbms项目实践)
package com.zcy.dao;
import com.zcy.pojo.User;
import java.util.List;
//MyBatis中 不称Dao接口,而是用Mapper
public interface UserMapper {
public List<User> getUserList();
}
3、接口实现类
由原来的UserDaoImpl.java,转变为一个Mapper配置文件
<mapper namespace="com.zcy.dao.UserMapper">
<select id="getUserList" resultType="com.zcy.pojo.User">
select * from mybatis.user
select>
mapper>
使用Junit测试,在test目录下创建和java相同的目录结构,然后新建一个测试类。
package com.zcy.dao;
import com.zcy.pojo.User;
import com.zcy.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void test(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
try{
//方式一:已过时
//List userList = sqlSession.selectList("com.zcy.dao.UserMapper.getUserList");
//方式二
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for(User user : userList)
System.out.println(user);
}catch (Exception e){
e.getStackTrace();
}finally {
sqlSession.close();
}
}
}
结果:
1、没有注册Mapper
解决:在mybatis-config.xml中注册对应的mapper,这里面的包的点要换成反斜杠,这是一个路径
<mappers>
<mapper resource="com/zcy/dao/UserMapper.xml"/>
mappers>
2、找不到UserMapper.xml
解决:IDEA的maven项目不会输出resources目录下的xml等资源,需要我们在pom.xml中设置,添加下列代码。
<build>
<resources>
<resource>
<directory>${basedir}/src/main/webappdirectory>
resource>
<resource>
<directory>${basedir}/src/main/resourcesdirectory>
resource>
<resource>
<directory>${basedir}/src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
<include>**/*.propertiesinclude>
includes>
resource>
resources>
build>
3、UserMapper.xml中的方法名或者类型不完整,namespace和resultType都需要完整路径。
<mapper namespace="com.zcy.dao.UserMapper">
<select id="getUserList" resultType="com.zcy.pojo.User">
select * from mybatis.user
select>
mapper>
<mapper namespace="com.zcy.dao.UserMapper">
<select id="getUserList" resultType="com.zcy.pojo.User">
select * from mybatis.user
select>
mapper>
namespace:命名空间,将整个命名空间绑定到UserMapper接口,包名要和Dao/Mapper接口的包名一致
select:查询语句
resultType:Sql语句执行后的返回值类型
#{ }里面放变量,如果参数类型是一个对象,则#{ }可以直接引用到该对象的成员
它们使用方法都一样
1、编写接口
package com.zcy.dao;
import com.zcy.pojo.User;
import java.util.List;
public interface UserMapper {
//查询全部用户
public List<User> getUserList();
//通过ID查询用户
public User getUserById(int id);
//增加用户
public int addUser(String name, String password);
//更新用户
public int updateUser(int id);
//删除用户
public int delete(int id);
}
2、编写对应mapper中的sql语句
<mapper namespace="com.zcy.dao.UserMapper">
<select id="getUserList" resultType="com.zcy.pojo.User">
select * from mybatis.user;
select>
<select id="getUserById" parameterType="int" resultType="com.zcy.pojo.User">
select * from mybatis.user where id = #{id};
select>
<insert id="addUser" parameterType="com.zcy.pojo.User">
insert into mybatis.user (id, name, password) values (#{id}, #{name}, #{password});
insert>
<update id="updateUser" parameterType="com.zcy.pojo.User">
update mybatis.user set name=#{name}, passwod=#{passwod} where id = #{id};
update>
<delete id="deleteUser" parameterType="com.zcy.pojo.User">
delete from mybatis.user where id = #{id};
delete>
mapper>
3、测试
增删改需要提交事务,才能修改数据库!!
package com.zcy.dao;
import com.zcy.pojo.User;
import com.zcy.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test
public void testDelete(){
//固定写法
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//删除用户
userMapper.deleteUser(3);
//涉及到增删改都需要提交事务
sqlSession.commit();
List<User> userList = userMapper.getUserList();
for(User u : userList)
System.out.println(u);
//固定写法
sqlSession.close();
}
@Test
public void testSelect(){
//固定写法
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//通过ID查询用户
User user = userMapper.getUserById(1);
System.out.println(user);
//固定写法
sqlSession.close();
}
@Test
public void testInsert(){
//固定写法
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//增加新用户
User user = new User(4, "新增用户", "111111");
userMapper.addUser(user);
//增删改必须提交事务,才能改变数据库
sqlSession.commit();
List<User> userList = userMapper.getUserList();
for(User u : userList)
System.out.println(u);
//固定写法
sqlSession.close();
}
@Test
public void testUpdate(){
//固定写法
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//更新用户
User user = new User(1, "更新的用户", "123456");
userMapper.updateUser(user);
//增删改必须提交事务,才能改变数据库
sqlSession.commit();
List<User> userList = userMapper.getUserList();
for(User u : userList)
System.out.println(u);
//固定写法
sqlSession.close();
}
}
也可以设置为自动提交,在获得SqlSession时,添加参数true。
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
在写mapper.xml的SQL语句时,我们有多种方式获取接口中方法的参数:
仅一个基本类型参数时,可直接省略
接口中的方法 public User getUserById(int id);
maaper中sql:省略 parameterType="int"
<select id="getUserById" resultType="com.zcy.pojo.User">
select * from mybatis.user where id = #{id};
</select>
仅一个参数且是对象时,直接用类
接口中的方法 public int addUser(User user);
mapper中的sql:其中 id、name、password都是User类的同名成员变量,名字必须相同
<insert id="addUser" parameterType="com.zcy.pojo.User">
insert into mybatis.user (id, name, password) values (#{id}, #{name}, #{password});
</insert>
更好的替代方式!用Map传递参数,取代对象传递参数!
接口中的方法:public int getUserById2(Map<String, Object> map);
mapper中的sql:不用成员变量来获取参数,而是用map的key(相当于可以自定义),并且不需要写完整的User成员
<select id="getUserById2" parameterType="map" resultType="com.zcy.pojo.User">
select * from mybatis.user where id = #{userId} and name = #{userName};
</select>
测试类中的写法:
@Test
public void getUserById2(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<String, Object>();
map.put("userId",2);
map.put("userName","小红");
User user = userMapper.getUserById2(map);
System.out.println(user);
sqlSession.close();
}
方法一:在Java代码执行的时候,传递通配符% %
UserMapper.xml
<!-- 模糊 查询 -->
<select id="getUserLike" resultType="com.zcy.pojo.User">
select * from user where name like #{value};
</select>
@Test
public void testSelectLike(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserLike("%小%");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
方法二:在mapper的sql中使用通配符
UserMapper.xml
<select id="getUserLike" resultType="com.zcy.pojo.User">
select * from user where name like "%" #{value} "%";
select>
@Test
public void testSelectLike(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserLike("小");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}