一.pom.xml
<dependencies>
<!-- mybatis-plus插件依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.1</version>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.11</version>
</dependency>
<!--简化bean代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
二.mybatis主配置文件【url访问路径】
1.图解
2.范例
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/db2?
useUnicode=true&characterEncoding=utf8&
autoReconnect=true&allowMultiQuerie s=true&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
三.日志文件
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%t] [%c]-[%p] %m%n
四.映射配置文件
1.图解
2.范例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mapper.UserMapper">
<select id="findAll" resultType="pojo.User">
select * from tb_user
</select>
</mapper>
五.实体类
1.图解
2.范例
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor //空参构造方法
@AllArgsConstructor //全参构造方法
@TableName("tb_user")
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
六.dao层
1.图解
2.范例
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import pojo.User;
import java.util.List;
/**
* mybatisPlus整合mybatis
*/
public interface UserMapper extends BaseMapper<User> {
public List<User> findAll();
}
七.测试类
1.使用mybatis方式
(1)代码块
import mapper.UserMapper;
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 org.junit.Test;
import pojo.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 使用mybatis创建对象
*/
public class UserMapperTest {
@Test
public void test1() throws IOException {
String resource = "mybatis-config.xml";
//获取配置文件位置
InputStream in = Resources.getResourceAsStream(resource);
//创建工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//获取执行sqlsession对象
SqlSession sqlSession = factory.openSession();
//获取代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> users = mapper.findAll();
for (User user:users) {
System.out.println(user);
}
}
}
(2)范例
2.使用mybatisPlus方式
(1)代码块
import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import mapper.UserMapper;
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 org.junit.Test;
import pojo.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* 测试mybatisPlus创建sqlsession对象
*/
public class MybatisPlusTest {
@Test
public void test1() throws IOException {
String resource = "mybatis-config.xml";
//获取配置文件位置
InputStream in = Resources.getResourceAsStream(resource);
//使用MybatisPlusBuilder对象创建工厂
SqlSessionFactory factory = new MybatisSqlSessionFactoryBuilder().build(in);
//获取执行sqlsession对象
SqlSession sqlSession = factory.openSession();
//获取代理对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//参数是查询条件,null是没有查询条件
List<User> users = mapper.selectList(null); //mybatisPlus提供的方法
for (User user:users) {
System.out.println(user);
}
}
}
(2)范例
八.源码
mybatis-plus.rar