添加Maven依赖:
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.10version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.13version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.12version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-typehandlers-jsr310artifactId>
<version>1.0.2version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-coreartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-extensionartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-annotationartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>5.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.2.1.RELEASEversion>
<scope>testscope>
dependency>
<configuration status="error">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
Console>
<RollingFile name="RollingFile" fileName="logs/web.log"
filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
<SizeBasedTriggeringPolicy size="2MB"/>
RollingFile>
appenders>
<loggers>
<root level="trace">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
root>
loggers>
configuration>
driverClass=com.mysql.cj.jdbc.Driver
connectionURL=jdbc:mysql://127.0.0.1/db_test?useSSL=false&serverTimezone=GMT&nullCatalogMeansCurrent=true
userId=root
password=root
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 不支持缓存 -->
<setting name="cacheEnabled" value="false"/>
<!-- 设置等待数据库响应时间,单位秒 -->
<setting name="defaultStatementTimeout" value="5"/>
<!-- 允许JDBC支持生成主键 -->
<setting name="useGeneratedKeys" value="true"/>
<setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 打印查询语句 -->
<setting name="mapUnderscoreToCamelCase" value="true"/><!-- mybatis下划线转驼峰式 -->
</settings>
<!-- 别名 -->
<typeAliases>
<package name="com.hc.bean"/>
</typeAliases>
<!-- 插件配置项 -->
<plugins>
<!-- 分页查询插件 -->
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
</plugin>
</plugins>
</configuration>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:mysql.properties"/>
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driverClass}">property>
<property name="url" value="${connectionURL}">property>
<property name="username" value="${userId}">property>
<property name="password" value="${password}">property>
<property name="filters" value="stat"/>
bean>
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<property name="dbConfig">
<bean class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
<property name="tableUnderline" value="true">property>
<property name="idType" value="AUTO"/>
<property name="tablePrefix" value="tb_"/>
bean>
property>
bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="ds"/>
<property name="mapperLocations" value="classpath*:mapper/*.xml"/>
<property name="configLocation" value="classpath:mybatisplus-cfg.xml"/>
<property name="globalConfig" ref="globalConfig">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hc.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="ds"/>
bean>
<tx:annotation-driven transaction-manager="txManager"/>
beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.hc"/>
<import resource="mybatisplus.xml"/>
beans>
@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "db_test.tb_emp")
public class Emp extends Model<Emp> implements Serializable { //继承Model类,是为了实现AR功能
private static final long serialVersionUID = -1696472017703928733L;
@TableId(value = "empno", type = IdType.INPUT)
private Integer empno;
@TableField(value = "ename")
private String ename;
@TableField(value = "job")
private String job;
@TableField(value = "mgr")
private Integer mgr;
@TableField(value = "hiredate")
private LocalDate hiredate;
@TableField(value = "sal")
private BigDecimal sal;
@TableField(value = "comm")
private BigDecimal comm;
@TableField(value = "deptno")
private Integer deptno;
}
public interface EmpMapper extends BaseMapper<Emp> {
}
<mapper namespace="com.hc.dao.EmpMapper">
<resultMap id="BaseResultMap" type="com.hc.bean.Emp">
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<result column="mgr" property="mgr"/>
<result column="hiredate" property="hiredate"/>
<result column="sal" property="sal"/>
<result column="comm" property="comm"/>
<result column="deptno" property="deptno"/>
resultMap>
<sql id="Base_Column_List">
empno, ename, job, mgr, hiredate, sal, comm, deptno
sql>
mapper>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:mybatisplus.xml"})
public class EmpDaoMapperTest {
@Autowired
private EmpMapper empMapper;
@Test
public void insert() {
Emp emp = new Emp(1234, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
int insertRes = empMapper.insert(emp);
System.out.println(insertRes);
System.out.println(emp.getEmpno());//直接获取插入数据返回的自增主键值
}
@Test
public void insert1() {
Emp emp = new Emp(1234, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
emp = new Emp(1235, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
emp = new Emp(1236, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
emp = new Emp(1237, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
emp = new Emp(1238, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
emp = new Emp(1239, "zhangsan", "CLERK", 7788, LocalDate.of(1999, 9, 21), new BigDecimal(1200), new BigDecimal(300), 20);
empMapper.insert(emp);
}
@Test
public void update() {
Emp emp = new Emp(1235, "张三", "SALES", 7698, LocalDate.of(2018, 9, 21), new BigDecimal(1300), new BigDecimal(400), 20);
int updateRes = empMapper.updateById(emp);
System.out.println(updateRes);
}
@Test
public void delete1() {
int deleteRes = empMapper.deleteById(1235);
System.out.println(deleteRes);
}
@Test
public void delete2() {
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("ename", "张三");
int deleteRes = empMapper.delete(queryWrapper);
System.out.println(deleteRes);
}
@Test
public void delete3() {
int deleteRes = empMapper.deleteBatchIds(Arrays.asList(1236, 1237));
System.out.println(deleteRes);
}
@Test
public void delete4() {
Map<String, Object> map = new HashMap<>();
map.put("ename", "zhangsan");
int deleteRes = empMapper.deleteByMap(map);
System.out.println(deleteRes);
}
@Test
public void select1() {
Emp emp = empMapper.selectById(7788);
System.out.println(emp);
}
@Test
public void select2() {
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("ename", "scott");
queryWrapper.eq("deptno", "20");
Emp emp = empMapper.selectOne(queryWrapper);
System.out.println(emp);
}
@Test
public void select3() {
QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
queryWrapper.like("ename", "%AR%");
List<Emp> empList = empMapper.selectList(queryWrapper);
for (Emp emp : empList) {
System.out.println(emp);
}
}
@Test
public void select4() {
List<Emp> empList = empMapper.selectBatchIds(Arrays.asList(7369, 7782, 7844));
for (Emp emp : empList) {
System.out.println(emp);
}
}
@Test
public void select5() {
Map<String, Object> map = new HashMap<>();
map.put("ename", "zhangsan");
map.put("job", "clerk");
List<Emp> empList = empMapper.selectByMap(map);
for (Emp emp : empList) {
System.out.println(emp);
}
}
@Test
public void select6() {
IPage<Emp> empPage = empMapper.selectPage(new Page<>(2, 2), null);
for (Emp emp : empPage.getRecords()) {
System.out.println(emp);
}
}
@Test
public void select7() {
Wrapper<Emp> queryWrapper = new QueryWrapper<Emp>()
.between("sal", 1500, 3500)
.like("ename", "%A%");
IPage<Emp> empPage = empMapper.selectPage(new Page<>(1, 3), queryWrapper);
for (Emp emp : empPage.getRecords()) {
System.out.println(emp);
}
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:mybatisplus.xml"})
public class EmpDaoARTest {
@Test
public void insert(){
Emp emp = new Emp(1230,"zhangsan","CLERK",7788, LocalDate.of(1999,9,21),new BigDecimal(1200),new BigDecimal(300),20);
boolean insertRes = emp.insert( );
System.out.println(insertRes);
System.out.println(emp.getEmpno());//直接获取插入数据返回的自增主键值
}
@Test
public void update(){
Emp emp = new Emp(1230,"张三","SALES",7521, LocalDate.of(2018,9,21),new BigDecimal(1400),new BigDecimal(400),10);
boolean updateRes = emp.updateById();
System.out.println(updateRes);
}
@Test
public void delete1(){
Emp emp = new Emp();
boolean deleteRes1 = emp.deleteById(1230);
System.out.println(deleteRes1);
emp.setEmpno(1234);
boolean deleteRes2 = emp.deleteById();
System.out.println(deleteRes2);
}
@Test
public void delete2(){
Emp emp = new Emp();
QueryWrapper<Emp> queryWrapper =new QueryWrapper<>();
queryWrapper.ge("empno",7902);
boolean res = emp.delete(queryWrapper);
System.out.println(res);
}
@Test
public void select1(){
Emp emp = new Emp();
Emp emp1 = emp.selectById(7788);
System.out.println(emp1);
emp.setEmpno(7788);
Emp emp2 = emp.selectById();
System.out.println(emp2);
}
@Test
public void select2(){
List<Emp> empList = new Emp().selectAll();
for (Emp item : empList) {
System.out.println(item);
}
}
@Test
public void select3(){
QueryWrapper<Emp> queryWrapper = new QueryWrapper<Emp>().like("ename","%SCOTT%");
Emp emp = new Emp();
List<Emp> empList = emp.selectList(queryWrapper);
for (Emp item : empList) {
System.out.println(item);
}
Integer count = emp.selectCount(queryWrapper);//参数值为null时表示查看全表
System.out.println(count);
}
@Test
public void select4(){
Wrapper<Emp> queryWrapper = new QueryWrapper<Emp>().lt("sal",1500);
IPage<Emp> empPage = new Emp().selectPage(new Page<>(2, 2), queryWrapper);
for (Emp emp :empPage.getRecords()){
System.out.println(emp);
}
System.out.println(empPage.getTotal());
System.out.println(empPage.getCurrent());
System.out.println(empPage.getPages());
System.out.println(empPage.getSize());
System.out.println(empPage.offset());
}
}