根据狂神老师视频写的笔记
org.mybatis
mybatis
3.5.2
数据持久化
为什么需要持久化?
帮助程序员将数据存入数据库中
方便
传统的JDBC代码太复杂了 ,简化成框架实现自动化
Mybatis优点?
提供映射标签,支持对象与数据库的orm字段关系映射。
提供对象关系映射标签,支持对象关系组建维护。
提供xml标签,支持编写动态sql。
思路:搭建环境-->导入Mybatis-->编写代码-->测试
CREATE DATABASE `mybatis`;
USE `mybatis`;
CREATE TABLE `user`(
`id` INT(20) NOT NULL PRIMARY KEY,
`name` VARCHAR(30) DEFAULT NULL,
`pwd` VARCHAR(30) DEFAULT NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `user`(`id`,`name`,`pwd`) VALUES
(1,'九七','123456'),
(2,'张三','123456'),
(3,'李四','123456');
新建项目
4.0.0
org.example
MybatisStudy
pom
1.0-SNAPSHOT
Mybatis-01
mysql
mysql-connector-java
5.1.47
org.mybatis
mybatis
3.5.2
junit
junit
4.12
src/main/resources
**/*.properties
**/*.xml
false
src/main/java
**/*.properties
**/*.xml
false
核心配置文件
编写mybatis工具类
package com.jiuqi.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;
//SqlSessionFactory-->sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
//使用Mybatis第一步:获取SqlSessionFactory对象
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。SqlSession 提供了在数据库执行 SQL 命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
实体类
package com.jiuqi.pojo;
//实体类
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
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;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
Dao接口
import com.jiuqi.pojo.User;
import java.util.List;
public interface UserDao {
List getUserList();
}
接口实现类(和web不同的是由原来的UserDaoImpl转变为Mapper配置文件)
src/main/resources
**/*.properties
**/*.xml
false
src/main/java
**/*.properties
**/*.xml
false
package com.jiuqi.dao;
import com.jiuqi.pojo.User;
import com.jiuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test() {
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//方法一:执行Sql
UserDao userDao = sqlSession.getMapper(UserDao.class);
List userList = userDao.getUserList();
/*
方式二(了解不需要掌握)
List userList = sqlSession.selectList("com.jiuqi.dao.UserDao.getUserList");
*/
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭SqlSession
sqlSession.close();
}
}
}
注意:增删改要提交事务
namespace中的包名要和接口的包名一致
选择,查询
编写接口
//根据ID查询用户
User getUserById(int id);
编写对应的SQL语句
测试
@Test
public void getUserById() {
//第一步:获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//方法一:执行Sql
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User userById = userMapper.getUserById(1);
System.out.println(userById);
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭SqlSession
sqlSession.close();
}
}
编写接口
int addUser(User user);
编写对应的SQL语句
insert into mybatis.user (id, name, pwd) value (#{id},#{name},#{pwd});
测试
@Test
public void addUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper Mapper = sqlSession.getMapper(UserMapper.class);
int name = Mapper.addUser(new User(4, "赵六", "123456"));
if(name > 0){
System.out.println("插入成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
编写接口
int updateUser(User user);
编写对应的SQL语句
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
测试
@Test
public void updateUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int name = mapper.updateUser(new User(4, "刘能", "111111"));
sqlSession.commit();
sqlSession.close();
}
编写接口
int deleteUser(int id);
编写对应的SQL语句
delete from mybatis.user where id=#{id}
测试
@Test
public void deleteUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
编写接口
int addUser2(Map map);
编写对应的SQL语句
insert into mybatis.user (id, name, pwd) value (#{userid},#{username},#{userpwd});
测试
@Test
public void addUser2() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper Mapper = sqlSession.getMapper(UserMapper.class);
Map map = new HashMap();
map.put("userid",5);
map.put("username","春风");
map.put("userpwd","223113");
int name = Mapper.addUser2(map);
if(name > 0){
System.out.println("插入成功");
}
//提交事务
sqlSession.commit();
sqlSession.close();
}
List userLike = Mapper.getUserLike("赵%");
select * from mybatis.user where name like "%"#{value}"%"
编写接口
List getUserLike(String name);
编写对应的SQL语句
测试
@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();
}
configuration(配置)
properties(属性)
settings(设置)
typeAliases(类型别名)
typeHandlers(类型处理器)
objectFactory(对象工厂)
plugins(插件)
environments(环境配置)
environment(环境变量)
transactionManager(事务管理器)
dataSource(数据源)
databaseIdProvider(数据库厂商标识)
mappers(映射器)
MyBatis 可以配置成适应多种环境
不过要记住:尽管可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境。
学会使用多套运行环境
每个数据库对应一个 SqlSessionFactory 实例
Mybatis默认的事务管理器是JDBC(MANAGED),连接池:POOLED
我们可以通过properties属性来实现引用配置文件
这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置[db.properties]
编写配置文件
db.peoperties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=201408181
在核心配置文件中引入
xml的所有标签都有其顺序,如果不按照顺序会报错(properties,settings,typeAliases,typeHandlers,objectWrapperFactory,reflectorFactory,plugins, environments,databaseldProvider,mappers)
也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
在实体类少的时候使用第一种方式,实体类多的情况下使用第二种,但第一种可以自定义别名,第二种则不行,但可以通过实体类增加注解更改
@Alias("hello")
//实体类
public class User {
这是Mybatis中极为重要的调整设置,它会改变Mybatis的运行时行为
cacheEnabled | 全局性地开启或关闭所有映射器配置文件中已配置的任何缓存。 | true | false | true |
lazyLoadingEnabled | 延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。 特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。 |
true | false | false |
logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J | LOG4J(deprecated since 3.5.9) | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING | 未设置 |
mybatis-generator-core
mybatis-plus
通用mapper
是代码更加简洁但一般不用
推荐使用方式一
MapperRegistry:注册绑定我们的Mapper文件
方式一:
方式二:使用class文件绑定注册
注意点:
方式三:使用包扫描进行注入绑定
注意点:
不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题。
SqlSessionFactoryBuilder:
SqlSessionFactory :
SqlSession :
public class User {
private int id;
private String name;
private String password;
(1).解决方法:起别名
结果集映射
resultMap
元素是 MyBatis 中最重要最强大的元素ResultMap
的优秀之处——你完全可以不用显式地配置它们如果一个数据库操作出现了异常,我们需要排错,日志就是最好的助手
以前可以用sout,debug进行排错,现在我们可以使用日志工厂
logImpl | 指定 MyBatis 所用日志的具体实现,未指定时将自动查找。 | SLF4J | LOG4J(deprecated since 3.5.9) | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING | STDOUT_LOGGING | NO_LOGGING | 未设置 |
在Mybatis具体使用那个日志实现,需要在设置中设定
STDOUT_LOGGING标准日志输出
在mybits核心配置文件中,配置我们的日志
Opening JDBC Connection
Created connection 614685048.
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@24a35978]
==> Preparing: select * from mybatis.user where id=?
==> Parameters: 1(Integer)
<== Columns: id, name, pwd
<== Row: 1, 九七, 123456
<== Total: 1
User{id=1, name='九七', password='123456'}
什么是Log4j?
先导入log4j的包
log4j
log4j
1.2.17
2.log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/jiuqi.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
配置log4j为日志实现
Log4j使用
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.logging.LogFactory]-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.datasource.pooled.PooledDataSource]-PooledDataSource forcefully closed/removed all connections.
[org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VFS
[org.apache.ibatis.io.JBoss6VFS]-JBoss 6 VFS API is not available in this environment.
[org.apache.ibatis.io.VFS]-Class not found: org.jboss.vfs.VirtualFile
[org.apache.ibatis.io.VFS]-VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
[org.apache.ibatis.io.VFS]-Using VFS adapter org.apache.ibatis.io.DefaultVFS
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/java/MybatisStudy/Mybatis-04/target/test-classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/java/MybatisStudy/Mybatis-04/target/test-classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Reader entry: UserDaoTest.class
[org.apache.ibatis.io.DefaultVFS]-Listing file:/D:/java/MybatisStudy/Mybatis-04/target/test-classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/java/MybatisStudy/Mybatis-04/target/test-classes/com/jiuqi/dao/UserDaoTest.class
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/java/MybatisStudy/Mybatis-04/target/test-classes/com/jiuqi/dao/UserDaoTest.class
[org.apache.ibatis.io.DefaultVFS]-Reader entry: ���� 4 9
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Reader entry: UserMapper.class
[org.apache.ibatis.io.DefaultVFS]-Reader entry: UserMapper.xml
[org.apache.ibatis.io.DefaultVFS]-Listing file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao/UserMapper.class
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao/UserMapper.class
[org.apache.ibatis.io.DefaultVFS]-Reader entry: ���� 4 getUserById (I)Lcom/jiuqi/pojo/User;
[org.apache.ibatis.io.DefaultVFS]-Find JAR URL: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao/UserMapper.xml
[org.apache.ibatis.io.DefaultVFS]-Not a JAR: file:/D:/java/MybatisStudy/Mybatis-04/target/classes/com/jiuqi/dao/UserMapper.xml
[org.apache.ibatis.io.DefaultVFS]-Reader entry:
[org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.jiuqi.dao.UserDaoTest matches criteria [is assignable to Object]
[org.apache.ibatis.io.ResolverUtil]-Checking to see if class com.jiuqi.dao.UserMapper matches criteria [is assignable to Object]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Opening JDBC Connection
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Created connection 1772160903.
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@69a10787]
[com.jiuqi.dao.UserMapper.getUserById]-==> Preparing: select * from mybatis.user where id=?
[com.jiuqi.dao.UserMapper.getUserById]-==> Parameters: 1(Integer)
[com.jiuqi.dao.UserMapper.getUserById]-<== Total: 1
User{id=1, name='九七', password='123456'}
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@69a10787]
[org.apache.ibatis.transaction.jdbc.JdbcTransaction]-Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@69a10787]
[org.apache.ibatis.datasource.pooled.PooledDataSource]-Returned connection 1772160903 to pool.
简单使用
1.在要使用Log4j的类中,导入 import org.apache.log4j.Logger; 倒错会报错
2.日志对象,参数为当前类的class
Logger logger = Logger.getLogger(UserDaoTest.class);
3.日志级别
logger.info("info进入testLog4j");
logger.debug("debug:进入testLog4j");
logger.error("error:进入testLog4j");
语法格式:SELECT * from user limit startIndex,pageSize
SELECT * from user limit 3; #[0,n]
List getUserByLimit(Map map);
@Test
public void getUserByLimit() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap map = new HashMap();
map.put("startIndex",0);
map.put("pageSize",2);
List userByLimit = mapper.getUserByLimit(map);
for (User user : userByLimit) {
System.out.println(user);
}
sqlSession.close();
}
不再使用SQL进行分页
List getUserByRowRounds();
@Test
public void getUserByRowRounds() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//RowBounds实现
RowBounds rowBounds = new RowBounds(1, 2);
//通过Java代码的方式实现分页
List userList = sqlSession.selectList("com.jiuqi.dao.UserMapper.getUserByRowRounds",null,rowBounds);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
以后工作可能会用
真正的开发中我们一般选择面向接口编程
为什么面向接口编程?
在面向的对象的系统中,系统的各级功能都有许许多多的不同对象协作完成,这种情况下各个对象内部是如何实现自己的对系统设计人员来讲就不那么重要了
而各个对象之间的协作关系则成为系统设计的关键,小到不同类之间通信,大到各模块之间的交互,在系统设计之初都是要着重考虑,这也是系统设计的主要内容,面向接口编程就是按照这种思想来编程。
关于接口的理解
接口应有两类:第一类是对一个个体的抽象,他可对应为一个抽象体
第二类是对一个个体某一方面的抽象,即形成一个抽象面
一个个体有可能有多个抽象面,抽象体和抽象面是有区别的
三个面向的区别
本质:反射机制实现
底层:动态代理
注解在接口上实现
@Select("select * from user")
List getUsers();
需要在核心配置文件中绑定接口
测试
@Test
public void getUsers() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List users = userMapper.getUsers();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
我们可以在工具类(MybatisUtils)创建的时候自动提交事务
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口时增加注解
@Select("select * from user")
List getUsers();
//方法存在多个参数,所有参数前面必须加@Parameter()注解
@Select("select * from user where id = #{id}")
User getUserById(@Param("id") int id);
@Insert ("insert into user(id,name,pwd) values (#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},id=#{id},pwd=#{pwd} where id=#{id}")
int updateUser(User user);
@Delete("delete from user where id = #{id}")
int deleteUser(@Param("id") int id);
测试
关于@Param()注解
@Test
public void getUsers() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List users = userMapper.getUsers();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.addUser(new User(7, "白也", "123456"));
sqlSession.close();
}
@Test
public void updateUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.updateUser(new User(7,"白也","999999"));
sqlSession.close();
}
@Test
public void deleteUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
//底层只要应用反射机制
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(7);
sqlSession.close();
}
注意:我们一定要将接口注册绑定到我们配置文件上
Lombok项目是一个Java库,它会自动插入编辑器和构建工具中,Lombok提供了一组有用的注释,用来消除Java类中的大量样板代码。仅五个字符(@Data)就可以替换数百行代码从而产生干净,简洁且易于维护的Java类
@Setter :注解在类或字段,注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法。
@Getter :使用方法同上,区别在于生成的是getter方法。
@ToString :注解在类,添加toString方法。
@EqualsAndHashCode: 注解在类,生成hashCode和equals方法。
@NoArgsConstructor: 注解在类,生成无参的构造方法。
@RequiredArgsConstructor: 注解在类,为类中需要特殊处理的字段生成构造方法,比如final和被@NonNull注解的字段。
@AllArgsConstructor: 注解在类,生成包含类中所有字段的构造方法。
@Data: 注解在类,生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
@Slf4j: 注解在类,生成log变量,严格意义来说是常量。
使用步骤:
org.projectlombok
lombok
1.18.22
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String pwd;
}
CREATE TABLE `teacher` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO teacher(`id`, `name`) VALUES `teacher`(1, '秦老师');
CREATE TABLE `student` (
`id` INT(10) NOT NULL,
`name` VARCHAR(30) DEFAULT NULL,
`tid` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fktid` (`tid`),
CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('1', '小明', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('2', '小红', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('3', '小张', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('4', '小李', '1');
INSERT INTO `student` (`id`, `name`, `tid`) VALUES ('5', '小王', '1');
org.projectlombok
lombok
1.18.22
@Data
public class Student {
private int id;
private String name;
//学生需要关联一个老师
private Teacher teacher;
}
@Data
public class Teacher {
private int id;
private String name;
}
public interface StudentMapper {
//查询所有学生的信息以及对应的老师信息
public List getStudent();
public List getStudent2();
}
public interface TeacherMapper {
@Select("select * from Teacher where id=#{tid}")
Teacher getTeacher(@Param("tid") int id);
}
@Test
public void TeacherMapper() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
@Test
public void getStudent() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Test
public void getStudent2() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
sqlSession.close();
}
@Data
public class Teacher {
private int id;
private String name;
//一个老师拥有多个学生
private List students;
}
@Data
public class Student {
private int id;
private String name;
private int tid;
}
//获取老师的信息以及老师下所有学生的信息
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher2(@Param("tid") int id);
public List getStudent();
建立Mapper.xml文件
@Test
public void getTeacher() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher(1);
System.out.println(teacher);
sqlSession.close();
}
@Test
public void getTeacher2() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
Teacher teacher = mapper.getTeacher2(1);
System.out.println(teacher);
sqlSession.close();
}
1.关联-association【多对一】
2.集合-collection【一对多】
3.javaType & ofType?
注意:
面试高频:
SQL 语句拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号,利用动态 SQL,可以彻底摆脱这种痛苦。
CREATE TABLE `blog`(
`id` VARCHAR(50) NOT NULL COMMENT '博客id',
`title` VARCHAR(100) NOT NULL COMMENT '博客标题',
`author` VARCHAR(30) NOT NULL COMMENT '博客作者',
`create_time` DATETIME NOT NULL COMMENT '创建时间',
`views` INT(30) NOT NULL COMMENT '浏览量'
)ENGINE=INNODB DEFAULT CHARSET=utf8
1.导包
2.编写配置文件
3.编写实体类
public class Blog {
private int id;
private String title;
private String author;
private Date create_time;
private int views;
}
public static String getId(){
return UUID.randomUUID().toString().replaceAll("-", "");
}
4.编写实体类对应的Mapper和Mapper.xml文件
public interface BlogMapper {
//插入数据
int addBolg(Blog blog);
}
insert into mybatis.blog(id, title, author, create_time, views)
values (#{id}, #{title}, #{author}, #{createTime}, #{views});
where后如果是and和or或者没有值会自动取消导致代码报错我们用where标签会解决这个问题
我们也可以通过自定义 trim 元素来定制 where 元素的功能
...
set 元素会动态地在行首插入 SET 关键字,并会删掉额外的逗号
update mybatis.blog
title = #{title},
author = #{author}
where id = #{id}
...
所谓的动态SQL,本质还是SQL语句,只是我们在SQL层面去执行一个逻辑代码
有些时候我们会将一些功能的地方抽取出来,方便复用
1.使用sql标签抽取公共部分
title = #{title}
and author = #{author}
2.在需要使用的地方使用include标签引用
注意事项:
动态SQL的另外一个常用的操作需求是对一个集合进行遍历,通常在in条件语句
动态SQL就是在拼接SQL语句,我们只要保证SQL的正确性按照SQL的格式去排列组合就行了
什么是缓存
什么样的数据可以使用缓存?
Mybatis包含一个非常强大的查询缓存特性他可以非常方便的定制和配置缓存极大的提高查询效率
Mybatis系统中默认定义两级缓存:一级缓存和二级缓存
步骤:
Opening JDBC Connection
Created connection 1209669119.
==> Preparing: select * from mybatis.user where id = ?
==> Parameters: 1(Integer)
<== Columns: id, name, pwd
<== Row: 1, 九七, 123456
<== Total: 1
User(id=1, name=九七, pwd=123456)
User(id=1, name=九七, pwd=123456)
true
Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@481a15ff]
Returned connection 1209669119 to pool.
缓存失效的情况:
注意:一级缓存默认是开启的只在一次SqlSession中有效,也就是拿到链接倒关闭链接
一级缓存就是一个Map
二级缓存也叫全局缓存,一级缓存作用域太低所以产生二级缓存
基于nameSpace级别的缓存,一个名称空间对应一个二级缓存
工作机制:
步骤:
也可以自定义参数
问题:我们需要将实体类序列化否则就会报错
实体类没序列化的报错信息:java.io.NotSerializableException: com.jiuqi.pojo.User
小结:
缓存顺序:先看二级缓存有无查询数据--->再看一级缓存有没有--->最后查询数据库
Ehcache是一种广泛使用的开源Java分布式缓存,主要面向通用缓存
1.使用ehcache先导包
org.mybatis.caches
mybatis-ehcache
1.2.1
2.ehcache
3.在mapper指定使用我们的缓存实现
当然我们也可以自己写缓存,只继承cache也可以实现简单缓存,但要想写复杂的会非常难,没有必要深究,我们以后会用Redis数据库来做缓存
Bil实体类
package com.jiuqi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bill {
private Integer id; //id
private String billCode; //账单编码
private String productName; //商品名称
private String productDesc; //商品描述
private String productUnit; //商品单位
private BigDecimal productCount; //商品数量
private BigDecimal totalPrice; //总金额
private Integer isPayment; //是否支付
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate;//更新时间
private Integer providerId; //供应商ID
}
BillMapper
package com.jiuqi.dao.bill;
import com.jiuqi.pojo.Bill;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface BillMapper {
//根据供应商Id查询订单数量
int getBillCountByProviderId(@Param("providerId") Integer providerId);
//增加订单
int add(Map map);
//通过查询条件获取供应商列表-getBillList
List getBillList(Map map);
//通过条件查询,查询供货商数量
int getBillCount(@Param("productName") String productName,
@Param("providerId") String providerId,
@Param("isPayment") String isPayment
);
//通过delId删除Bill
int deleteBillById(@Param("id") Integer id);
//通过billId获取Bill
Bill getBillById(@Param("id") Integer id);
//修改订单信息
int modify(Map map);
//根据供应商Id删除订单信息
int deleteBillByProviderId(@Param("providerId") Integer providerId);
}
BillMapper.xml
insert into smbms.smbms_bill(id, billCode, productName, productDesc, productUnit,
productCount, totalPrice, isPayment, createdBy, creationDate,
modifyBy, modifyDate, providerId)
values(#{id}, #{billCode}, #{productName},#{productDesc}, #{productUnit}, #{productCount}, #{totalPrice},
#{isPayment}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate}, #{providerId})
delete from smbms.smbms_bill where id = #{id}
update smbms.smbms_bill
id = #{id},
billCode = #{billCode},
productName = #{productName},
productDesc = #{productDesc},
productUnit = #{productUnit},
productCount = #{productCount},
totalPrice = #{totalPrice},
isPayment = #{isPayment},
createdBy = #{createdBy},
creationDate = #{creationDate}
modifyBy = #{modifyBy},
modifyDate = #{modifyDate},
providerId = #{providerId}
where id = #{id}
delete from smbms.smbms_bill where providerId = #{providerId}
Bill测试
package com.jiuqi.dao;
import com.jiuqi.dao.bill.BillMapper;
import com.jiuqi.pojo.Bill;
import com.jiuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
public class BillMapperTest {
@Test
public void getBillCountByProviderId() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
int billCountByProviderId = mapper.getBillCountByProviderId(7);
System.out.println(billCountByProviderId);
sqlSession.close();
}
@Test
public void add() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
HashMap map = new HashMap();
map.put("id","99");
map.put("billCode","BILL2016_001");
map.put("productName","小麦果汁");
map.put("productDesc","饮料");
map.put("productUnit","瓶");
map.put("productCount","100");
map.put("totalPrice","1000");
map.put("isPayment","2");
map.put("createdBy","1");
try {
map.put("creationDate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-05-01 12:21:11"));
} catch (ParseException e) {
e.printStackTrace();
}
map.put("providerId","111");
mapper.add(map);
sqlSession.close();
}
@Test
public void getBillList() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
HashMap map = new HashMap();
map.put("productName","%麦%");
map.put("providerId","16");
map.put("isPayment","2");
map.put("from",0);
map.put("pageSize",9);
mapper.getBillList(map);
sqlSession.close();
}
@Test
public void getBillCount() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
mapper.getBillCount("小麦","16","2");
sqlSession.close();
}
@Test
public void deleteBillById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
mapper.deleteBillById(99);
sqlSession.close();
}
@Test
public void getBillById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
Bill billById = mapper.getBillById(1);
System.out.println(billById);
sqlSession.close();
}
@Test
public void modify() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
HashMap map = new HashMap();
map.put("id","1");
map.put("billCode","BILL2016_001");
map.put("productName","小麦果汁");
map.put("productDesc","饮料");
map.put("productUnit","瓶");
map.put("productCount","200");
map.put("totalPrice","6000");
map.put("isPayment","2");
map.put("createdBy","1");
mapper.modify(map);
sqlSession.close();
}
@Test
public void deleteBillByProviderId() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
mapper.deleteBillByProviderId(111);
sqlSession.close();
}
}
provider实体类
package com.jiuqi.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Provider {
private Integer id; //id
private String proCode; //供应商编码
private String proName; //供应商名称
private String proDesc; //供应商描述
private String proContact; //供应商联系人
private String proPhone; //供应商电话
private String proAddress; //供应商地址
private String proFax; //供应商传真
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate;//更新时间
private Bill bill;
}
ProviderMapper
package com.jiuqi.dao.provider;
import com.jiuqi.pojo.Provider;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface ProviderMapper {
//增加用户信息
public int add(Map map);
//通过条件查询providerList
public List getProviderList(Map map);
//获取供应商列表
public List getProList();
//通过条件查询供应商记录数
public int getProviderCount(@Param("proName") String proName,
@Param("proCode") String proCode);
//通过供应商Id删除供应商信息
public int deleteProviderById(@Param("id") Integer delId);
//根据供应商Id获取供应商信息
public Provider getProviderById(@Param("id") Integer id);
//修改供应商
public int modify(Map map);
}
ProviderMapper.xml
insert into smbms.smbms_provider(id, proCode, proName, proDesc, proContact, proPhone, proAddress, proFax, createdBy, creationDate, modifyDate, modifyBy)
values (#{id}, #{proCode}, #{proName}, #{proDesc}, #{proContact}, #{proPhone}, #{proAddress}, #{proFax}, #{createdBy}, #{creationDate}, #{modifyDate}, #{modifyBy})
delete from smbms.smbms_provider where id = #{id}
update smbms.smbms_provider set id = #{id}, proCode = #{proCode}, proName = #{proName}, proDesc = #{proDesc},
proContact = #{proContact}, proPhone = #{proPhone},proAddress = #{proAddress},
proFax = #{proFax}, createdBy = #{createdBy}, creationDate = #{creationDate},
modifyDate = #{modifyDate}, modifyBy = #{modifyBy} where id = #{id}
Provider测试类
package com.jiuqi.dao;
import com.jiuqi.dao.provider.ProviderMapper;
import com.jiuqi.pojo.Provider;
import com.jiuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class ProviderMapperTest {
@Test
public void add() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
HashMap map = new HashMap();
map.put("id",16);
map.put("proCode","JQ_GYS007");
map.put("proName","善感批发");
map.put("proDesc","长期合作伙伴,主营产品:百事可乐、小麦果汁、苹果、生活消费品");
map.put("proContact","韩驰");
map.put("proPhone","13796942060");
map.put("proAddress","北京市通州区");
map.put("proFax","0523-21299123");
map.put("createdBy",1);
map.put("creationDate",new Date());
mapper.add(map);
sqlSession.close();
}
@Test
public void getProviderList() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
HashMap map = new HashMap();
map.put("proName","善感");
map.put("proCode","JQ_GYS007");
map.put("from",0);
map.put("pageSize",9);
List providerList = mapper.getProviderList(map);
for (Provider provider : providerList) {
System.out.println(provider);
}
sqlSession.close();
}
@Test
public void getProList() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
mapper.getProList();
sqlSession.close();
}
@Test
public void getProviderCount() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
int count = mapper.getProviderCount("善感", "JQ_GYS007");
System.out.println(count);
sqlSession.close();
}
@Test
public void deleteProviderById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
mapper.deleteProviderById(16);
sqlSession.close();
}
@Test
public void getProviderById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
Provider providerById = mapper.getProviderById(16);
System.out.println(providerById);
sqlSession.close();
}
@Test
public void modify() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
ProviderMapper mapper = sqlSession.getMapper(ProviderMapper.class);
HashMap map = new HashMap();
map.put("id",16);
map.put("proCode","JQ_GYS007");
map.put("proName","善感批发");
map.put("proDesc","长期合作伙伴,主营产品:百事可乐、小麦果汁、苹果、生活消费品");
map.put("proContact","嘿嘿");
map.put("proPhone","13796942060");
map.put("proAddress","北京市朝阳区");
map.put("proFax","0523-21299123");
map.put("createdBy",1);
map.put("creationDate",new Date());
mapper.modify(map);
sqlSession.close();
}
}
Role实体类
package com.jiuqi.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class Role {
private Integer id; //id
private String roleCode; //角色编码
private String roleName; //角色名称
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate;//更新时间
}
RoleMapper
package com.jiuqi.dao.role;
import com.jiuqi.pojo.Role;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface RoleMapper {
//获取角色列表
public List getRoleList();
//增加角色信息
public int add(Role role);
//通过Id删除Role
public int deleteRoleById(@Param("id") Integer delId);
//修改角色信息
public int modify(Map map);
//通过Id获取role
public Role getRoleById(@Param("id") Integer id);
//根据roleCode,进行角色编码的唯一性验证
public int roleCodeIsExist(@Param("roleCode") String roleCode);
}
RoleMapper.xml
insert into smbms.smbms_role(id, roleCode, roleName, createdBy, creationDate, modifyBy, modifyDate)
values (#{id}, #{roleCode}, #{roleName}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate})
delete from smbms.smbms_role where id = #{id}
update smbms.smbms_role set id = #{id}, roleCode = #{roleCode}, roleName = #{roleName}, createdBy = #{createdBy},
creationDate = #{creationDate}, modifyBy = #{modifyBy}, modifyDate = #{modifyDate}
where id = #{id}
Role测试类
package com.jiuqi.dao;
import com.jiuqi.dao.role.RoleMapper;
import com.jiuqi.pojo.Role;
import com.jiuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public class RoleMapperTest {
@Test
public void getRoleList() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
List roleList = mapper.getRoleList();
for (Role role : roleList) {
System.out.println(role);
}
sqlSession.close();
}
@Test
public void add() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
Role role = new Role();
role.setId(4);
role.setRoleCode("SMBMS_JQDS");
role.setRoleName("总监");
role.setCreatedBy(1);
role.setCreationDate(new Date());
mapper.add(role);
sqlSession.close();
}
@Test
public void deleteRoleById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
mapper.deleteRoleById(4);
sqlSession.close();
}
@Test
public void modify() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
HashMap map = new HashMap();
map.put("id",4);
map.put("roleCode","SMBMS_JQDS");
map.put("roleName","产品经理");
try {
map.put("creationDate",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-09-07 14:26:34"));
} catch (ParseException e) {
e.printStackTrace();
}
mapper.modify(map);
sqlSession.close();
}
@Test
public void getRoleById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
System.out.println(mapper.getRoleById(4));
sqlSession.close();
}
@Test
public void roleCodeIsExist() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
int code = mapper.roleCodeIsExist("SMBMS_JQDS");
System.out.println(code);
sqlSession.close();
}
}
User实体类
package com.jiuqi.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private Integer age;//年龄
private String userRoleName; //用户角色名称
}
UserMapper
package com.jiuqi.dao;
import com.jiuqi.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
//通过userCode获取User
public User getLoginUser(@Param("userCode") String userCode);
//增加用户信息
public int add(Map map);
//通过条件查询userList
public List getUserList(@Param("userName") String userName,
@Param("userRole") Integer userRole,
@Param("from") Integer from,
@Param("pageSize") Integer pageSize);
//通过条件查询-用户记录数
public int getUserCount(@Param("userName") String userName,
@Param("userRole") Integer userRole);
//通过userId删除user
public int deleteUserById(@Param("id") Integer id);
//通过useId获取user
public User getUserById(@Param("id") Integer id);
//修改用户信息
public int modify(Map map);
//修改当前用户密码
public int updatePwd(@Param("id") Integer id, @Param("pwd") String pwd);
}
UserMapper.xml
insert into smbms.smbms_user(id, userCode, userName, userPassword, gender, birthday, phone,
address, userRole, createdBy, creationDate, modifyBy, modifyDate)
values(#{id}, #{userCode}, #{userName}, #{userPassword}, #{gender}, #{birthday}, #{phone},
#{address}, #{userRole}, #{createdBy}, #{creationDate}, #{modifyBy}, #{modifyDate})
delete from smbms.smbms_user where id = #{id}
update smbms.smbms_user set id = #{id}, userCode = #{userCode}, userName = #{userName}, userPassword = #{userPassword},
gender = #{gender}, birthday = #{birthday}, phone = #{phone}, address = #{address},
userRole = #{userRole}, createdBy = #{createdBy}, creationDate = #{creationDate}, modifyBy = #{modifyBy},
modifyDate = #{modifyDate} where id = #{id}
update smbms.smbms_user set serPassword = #{pwd} where id = #{id}
User测试类
package com.jiuqi.dao;
import com.jiuqi.pojo.User;
import com.jiuqi.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
public class UserMapperTest {
@Test
public void getLoginUser() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println(mapper.getLoginUser("admin"));
sqlSession.close();
}
@Test
public void add() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap map = new HashMap<>();
map.put("id",97);
map.put("userCode","fei");
map.put("userName","飞");
map.put("userPassword","999777");
map.put("gender",1);
try {
map.put("birthday",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-03-04 22:32:11"));
} catch (ParseException e) {
e.printStackTrace();
}
map.put("phone","13781588666");
map.put("address","江西省南昌市");
map.put("userRole",1);
map.put("createdBy",1);
map.put("creationDate",new Date());
mapper.add(map);
sqlSession.close();
}
@Test
public void getUserList() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.getUserList("邓超",null,0,9);
sqlSession.close();
}
@Test
public void getUserCount() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.getUserCount("邓超",null);
sqlSession.close();
}
@Test
public void deleteUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUserById(97);
sqlSession.close();
}
@Test
public void getUserById() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User userById = mapper.getUserById(1);
System.out.println(userById);
sqlSession.close();
}
@Test
public void modify() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap map = new HashMap();
map.put("id",97);
map.put("userCode","fei");
map.put("userName","飞");
map.put("userPassword","999666");
map.put("gender",1);
try {
map.put("birthday",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-03-04 22:32:11"));
} catch (ParseException e) {
e.printStackTrace();
}
map.put("phone","13781588666");
map.put("address","河南省郑州市");
map.put("userRole",1);
map.put("createdBy",1);
map.put("creationDate",new Date());
mapper.modify(map);
sqlSession.close();
}
@Test
public void updatePwd() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updatePwd(97,"123123");
sqlSession.close();
}
}