简介:
1.MyBatis-Plus,它是一个MyBatis的增强工具包,是简化MyBats开发工作。
2.资料地址:
官方地址:https://mp.baomidou.com/
资料地址:https://baomidou.gitee.io/mybatis-plus-doc/#/
文档地址:https://baomidou.gitee.io/mybatis-plus-doc/#/quick-start
集成MybatisPlus:
1.创建数据库表
2.创建实体类
@TableName(value=”tablename”)与数据库表明一致
@TableId(value=””,type=IdType.auto)实体类与数据库id一致的话就不用配括号里的值
@tableFiled(value=”columnName”,exist=false)数据库别的字段
package cn.xxs.pojo;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
/**
* 学生实体类
* @author xxs
*/
@TableName(value="tblstudent")
public class Student {
@TableId
private Integer studentId;//学生编号
@TableField
private String studentName;//学生姓名
private Integer age;//年龄
private String studentNo;//学号
private Date birthDay;//生日
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer studentId, String studentName, Integer age, String studentNo, Date birthDay) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.age = age;
this.studentNo = studentNo;
this.birthDay = birthDay;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" + studentName + ", age=" + age + ", studentNo="
+ studentNo + ", birthDay=" + birthDay + "]";
}
}
3、添加依赖
4.0.0
cn.xxs
spring_mybitsPlus
0.0.1-SNAPSHOT
1.8
4.3.7.RELEASE
1.8.1
1.6.6
3.2.8
5.1.45
2.9.4
com.baomidou
mybatis-plus
2.3
org.springframework
spring-context
${org.springframework-version}
commons-logging
commons-logging
org.springframework
spring-webmvc
${org.springframework-version}
org.springframework
spring-aop
${org.springframework-version}
org.springframework
spring-aspects
${org.springframework-version}
org.springframework
spring-beans
${org.springframework-version}
org.springframework
spring-context-support
${org.springframework-version}
org.springframework
spring-core
${org.springframework-version}
org.springframework
spring-expression
${org.springframework-version}
org.springframework
spring-instrument
${org.springframework-version}
org.springframework
spring-instrument-tomcat
${org.springframework-version}
org.springframework
spring-jdbc
${org.springframework-version}
org.springframework
spring-jms
${org.springframework-version}
org.springframework
spring-messaging
${org.springframework-version}
org.springframework
spring-orm
${org.springframework-version}
org.springframework
spring-oxm
${org.springframework-version}
org.springframework
spring-test
${org.springframework-version}
org.springframework
spring-tx
${org.springframework-version}
org.springframework
spring-web
${org.springframework-version}
org.springframework
spring-webmvc-portlet
${org.springframework-version}
org.springframework
spring-websocket
${org.springframework-version}
org.aspectj
aspectjrt
1.8.9
org.aspectj
aspectjweaver
1.8.1
commons-dbcp
commons-dbcp
1.4
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.5
commons-logging
commons-logging
1.2
commons-net
commons-net
3.3
commons-pool
commons-pool
1.5.5
com.fasterxml.jackson.core
jackson-annotations
${jackson-version}
com.fasterxml.jackson.core
jackson-core
${jackson-version}
com.fasterxml.jackson.core
jackson-databind
${jackson-version}
com.alibaba
fastjson
1.2.23
jstl
jstl
1.2
mysql
mysql-connector-java
${mysql-version}
org.slf4j
slf4j-api
${org.slf4j-version}
org.slf4j
jcl-over-slf4j
${org.slf4j-version}
runtime
org.slf4j
slf4j-log4j12
${org.slf4j-version}
runtime
log4j
log4j
1.2.17
javax.mail
mail
javax.jms
jms
com.sun.jdmk
jmxtools
com.sun.jmx
jmxri
runtime
javax.inject
javax.inject
1
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.1
provided
javax.servlet
jstl
1.2
junit
junit
4.7
test
maven-eclipse-plugin
2.9
org.springframework.ide.eclipse.core.springnature
org.springframework.ide.eclipse.core.springbuilder
true
true
org.apache.maven.plugins
maven-compiler-plugin
2.5.1
1.8
1.8
-Xlint:all
true
true
org.codehaus.mojo
exec-maven-plugin
1.2.1
org.test.int1.Main
alimaven
aliyun maven
http://maven.aliyun.com/nexus/content/groups/public/
true
false
4、创建mybatis-config.xml配置文件,放在resource下
5、创建jdbc.propertis配置文件,放在resource下
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
6、创建log4j.xml配置文件,放在resource下
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
7、创建bean.xml文件,放在resource下
这里我们需要 1》开启扫描
2》 加载jdbc配置文件
3》配置数据源
4》配置SqlSessionFaction(更换mybatisplus使用的SqlSessionFaction)
加载mybatis核心配置文件, 配置别名包扫描,注入全局策略到SqlSessionFactory
5》sql映射文件扫描
6》 定义MyBatisPlus的全局策略配置
dbColumnUnderline的值2.3版本以后,默认值为true这里如果你的数据库表的字段与实体类一样且不是t_id的形式,就为false。反之为true。 另外我们再配一个全局主键。
8.编写接口,继承BaseMapper接口
这里与mybatis最大的不同就是它不需要创建sql映射文件或者是写注解
public interface UserMapper extends BaseMapper{
}
9.创建 获取SqlSessionFactory工具类
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
sqlSessionFactory = (SqlSessionFactory) new ClassPathXmlApplicationContext("bean.xml").getBean("sqlSessionFactory");
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
10.创建测试类
/**
* 查询学生信息
* @throws IOException
*/
@org.junit.Test
public void selectById() throws IOException {
//通过输入流创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
//创建SqlSession对象
SqlSession ss = sqlSessionFactory.openSession();
//多态的运用,为掉方法做准备
UserMapper userMapper = ss.getMapper(UserMapper.class);
//查询学生信息
Student s = userMapper.selectById(4);
//控制台输出
System.out.println(s);
//释放资源
ss.close();
}
11.控制台打印结果:
由此可见,我们直接用方法selectById()就可以了,很方便。
除此之外,还有很多我们可以直接用的方法,比如insert,deleteById,updateById,selectBatchIds等。
当然了我们也可以根据自己所需自己在接口编写其他方法。