一、创建maven项目并添加依赖
1、在pom.xml文件中添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatispluspro</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
</dependencies>
</project>
二、配置log4j
1、配置 log4j.properties 文件
log4j.rootLogger = debug,stdout
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
2、在mybatis配置文件中加载log4j
<?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="logImpl" value="Log4j"/>
</settings>
</configuration>
三、配置数据库连接
1、配置 db.properties
db.driver = com.mysql.cj.jdbc.Driver
db.url= jdbc:mysql://localhost:3306/mydemo?serverTimezone=GMT%2B8
db.username = root
db.password = root
配置 spring
1、配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"></property>
<property name="jdbcUrl" value="${db.url}"></property>
<property name="user" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<bean id="dataSourceTransationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="dataSourceTransationManager"/>
<bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatiscfig.xml"></property>
<property name="typeAliasesPackage" value="entity"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapperdao"></property>
</bean>
</beans>
四、实体类
package entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import convertor.BitAndStringConvertor;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
@TableName(value = "userinfo")
public class User implements Serializable {
@TableId(value = "id")
private int id;
@TableField(select = false)
private List<UserBaseInfo> userBaseInfo;
@TableField(value = "sn")
private String sn;
@TableField(value = "sex",typeHandler = BitAndStringConvertor.class)
private String sex;
@TableField(value = "note")
private String note;
@TableField(value = "phone")
private String phone;
@TableField(value = "cn")
private String cn;
private Date modifydate;
@Override
public String toString() {
return "User{" +
", id=" + id +
", sn='" + sn + '\'' +
", note='" + note + '\'' +
", phone='" + phone + '\'' +
", cn='" + cn + '\'' +
", sex='" + sex + '\'' +
", modifydate=" + modifydate +
'}';
}
public List<UserBaseInfo> getUserBaseInfo() {
return userBaseInfo;
}
public void setUserBaseInfo(List<UserBaseInfo> userBaseInfo) {
this.userBaseInfo = userBaseInfo;
}
public Date getModifydate() {
return modifydate;
}
public void setModifydate(Date modifydate) {
this.modifydate = modifydate;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
}
五、Mapper接口
PersonMapper
package mapperdao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import entity.User;
import java.util.List;
public interface PersonMapper extends BaseMapper<User> {
}
Mapper接口徐继承 BaseMapper接口,并指定BaseMapper接口的泛型类型,如:BaseMapper<User> 。
六、项目截图
七、测试
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import entity.User;
import mapperdao.PersonMapper;
import myutils.LocalDateUtil;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
PersonMapper personMapper = context.getBean("personMapper", PersonMapper.class);
add(personMapper);
}
public static void add(PersonMapper personMapper){
User user=new User();
user.setCn("qw");
user.setSn("e");
user.setSex("男");
user.setModifydate(new java.sql.Date(LocalDateUtil.localDateTimeToDate(LocalDateUtil.getLocalDateTimeNow()).getTime()));
int insert = personMapper.insert(user);
System.out.println("insertRow:"+insert);
}
public static void delete(PersonMapper personMapper){
UpdateWrapper updateWrapper =new UpdateWrapper();
updateWrapper.eq("id",24);
int insert = personMapper.delete(updateWrapper);
System.out.println("insertRow:"+insert);
}
public static void update(PersonMapper personMapper){
User user=new User();
user.setCn("mm");
UpdateWrapper updateWrapper =new UpdateWrapper();
updateWrapper.eq("id",15);
int insert = personMapper.update(user,updateWrapper);
System.out.println("insertRow:"+insert);
}
public static void select(PersonMapper personMapper){
User user1 = personMapper.selectById(1);
System.out.println("insertRow:"+user1.toString());
}
}
|