MyBatis是一个持久层框架(即操作数据库),可以帮助我们省略JDBC
用法:就是在maven项目中导入MyBatis包
中文文档:mybatis – MyBatis 3 | 简介
先导知识:JavaWeb学习笔记(二) IDEA中创建maven项目以及导包方法
JavaWeb学习笔记(九)连接数据库
获取方式:
1. Github: Releases · mybatis/mybatis-3 · GitHub
2. maven仓库
1. 新建空的maven项目,删除src文件夹
2. 在pom.xml中导包:junit (用于单元测试); mysql-connector-java ; mybatis ;
3. 新建子模块:普通的maven项目
注意:新建完只有一定要为文件夹标记属性,否则不会报错
idea中try/catch快捷键:选中要包裹代码+ Ctrl + Alt +t
在Java文件夹下新建utils文件夹以及MybatisUtils类
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.InputStream;
public class MybatisUtils {
// 1.使用MyBatis获取sqlSessionFactory对象
private static SqlSessionFactory sqlSessionFactory;
static{
try {
String resource = "myBatis_config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
// 2.从 sqlSessionFactory 中获取SqlSession实例
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
5. 在MySQL中新建数据库、表格;略
略,这两步直接使用开头先导知识第二篇中的mybatis数据库中的user表以及实体类 User.java,但是要重写这个函数
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
UserMapper.java是一个接口类
package dao;//用于操控实体类 User.java
import pojo.User;
import java.util.List;
public interface UserMapper {
//声明getUserList方法,返回值是User列表
List getUserList();
}
UserMapper.xml 是一个配置文件,相当于接口实现类
namespace是实现了哪个接口,id是实现了接口里的哪个方法,resultType是返回值类型是哪个实体类
新建一个文件
每个接口实现类的xml文件都必须在这里注册
在test文件中写,文件夹的格式要和上面一样,并且都是一一对应的,每个dao都对应一个测试文件
package dao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pojo.User;
import utils.MybatisUtils;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
List userList = userDao.getUserList();
for(User user:userList){
System.out.println(user.toString());
}
sqlSession.close();
}
}
有三个文件受到影响,以下只显示新增部分
UserMapper.java
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
UserDaoTest.java
@Test
public void getUserById(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//只有这两步需要修改
User user= mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser(){
SqlSession sqlSession=MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(4, "发财", "1233"));
if(res>0){
System.out.println("success");
}
sqlSession.commit(); //增删改要提交事务
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"暴富","1237"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
UserMapper.xml
insert into mybatis.user (`userId`,`name`,`pwd`)values (#{userId},#{name},#{pwd});
update mybatis.user set name=#{name},pwd=#{pwd} where userId=#{userId};
delete from mybatis.user where userId=#{id};
UserMapper.java
//插入部分属性
int addUser2(Map map);
//通过多个属性共同查询
User getUserByIdAndName(Map map);
UserDaoTest.java
@Test //插入部分属性
public void addUserT2(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//
HashMap map = new HashMap<>();
map.put("userid",6);
map.put("passWord",12346);
//
mapper.addUser2(map);sqlSession.commit();
//
sqlSession.close();
}
@Test//通过单独的几个条件查询
public void getUserByIdAndNameTest(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//
HashMap map = new HashMap<>();
map.put("id",1);
map.put("name","小发");
//
User user = mapper.getUserByIdAndName(map);
System.out.println(user);
sqlSession.close();
}
UserMapper.xml
insert into mybatis.user(userId,pwd) values (#{userid},#{passWord})
UserMapper.java
List getUserLike(String value);
UserDaoTest.java
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List userLike = mapper.getUserLike("%小%");
for (User user : userLike) {
System.out.println(user);
}
sqlSession.close();
}
UserMapper.xml
下一篇:MyBatis学习笔记(二)根据配置文件优化