今天就隨便說說spring整合mybatis-plus,就再也不搭建一個web項目了,簡單作一個測試類。php
既然是spring,那就少不了各類xxx.xml配置文件。java
<1>. application-dao.xml dao層的配置,他的核心就是要產生Mapper代理對象mysql
一、數據源的配置
複製代碼
<context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK" />
複製代碼
二、數據源的配置
複製代碼
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
複製代碼
三、SqlSessionFactory
複製代碼
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="globalConfig" ref="globalConfig"></property>
<!-- 加載xxMapper.xml -->
<property name="mapperLocations">
<array>
<value>classpath:mapper/*Mapper.xml</value>
</array>
</property>
<!-- 配置分頁插件 -->
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
</bean>
</array>
</property>
</bean>
<!-- 聲明全局配置 -->
<bean id="globalConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig">
<!-- 指定主鍵自動增加類型 -->
<property name="dbConfig" ref="dbConfig"></property>
</bean>
<bean id="dbConfig" class="com.baomidou.mybatisplus.core.config.GlobalConfig.DbConfig">
<property name="idType" value="AUTO"></property>
</bean>
複製代碼
四、產生Mapper接口的代理對象
複製代碼
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 須要生成代理類對象的mapper接口包 -->
<property name="basePackage" value="com.xieyunjie.mapper"></property>
<!-- sqlSessionFactory 的name 用於爲代理類中生成SqlSession -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
複製代碼
<2>. application-service.xmlweb
<context:component-scan base-package="com.xieyunjie.service">
</context:component-scan>
複製代碼
<3>. applicationContext.xmlspring
<!-- 引入dao層的配置 -->
<import resource="classpath:application-dao.xml"/>
複製代碼
<4>. db.propertiessql
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
user=root
password=root
複製代碼
<5>. log4j.propertiesapache
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
複製代碼
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName(value="sys_user")//創建User.class和數據的sys_user表的關係
public class User implements Serializable{
private static final long serialVersionUID = 1L;
//字段名和表中的名字同樣時能夠不加如下註解,不一樣時須要加上該註解
@TableId(value="id") //表明它是主鍵
private Integer id;
@TableField(value="name")
private String name;
private String address;
private Date birth;
}
複製代碼
public interface UserMapper extends BaseMapper<User> {
}
複製代碼
<?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="com.xieyunjie.mapper.UserMapper" >
</mapper>
複製代碼
建立一個userMapper對象,進行測試mybatis
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
UserMapper userMapper=context.getBean(UserMapper.class);
複製代碼
private static void query5(UserMapper userMapper){
IPage<User> page=new Page<>(1,5);
userMapper.selectPage(page,null);
long total=page.getTotal();
System.out.println("總條數:"+total);
List<User> list=page.getRecords();
print(list);
}
複製代碼
private static void query4(UserMapper userMapper,String name){
Integer count=userMapper.selectCount(null);
QueryWrapper<User> queryWrapper=new QueryWrapper<>();
queryWrapper.like(name!=null,"name",name);
Integer selectCount=userMapper.selectCount(queryWrapper);
System.out.println(selectCount);
}
複製代碼
private static void query1(UserMapper userMapper){
User user=userMapper.selectById(3);
System.out.println(user);
}
複製代碼
private static void query3(UserMapper userMapper){
Map<String,Object> columnMap=new HashMap<>();
columnMap.put("name","小滎");
columnMap.put("address","南陽");
List<User> list=userMapper.selectByMap(columnMap);
print(list);
}
複製代碼
private static void query2(UserMapper userMapper){
//先放到一個集合裏面,最後進行查詢
Collection<Serializable> idList=new ArrayList<Serializable>();
idList.add(2);
idList.add(3);
idList.add(4);
List<User> list=userMapper.selectBatchIds(idList);
print(list);
}
複製代碼
private static void deleteUser(UserMapper userMapper){
//根據主鍵刪除
userMapper.deleteById(1);
//批量刪除。先放到一個集合裏面,而後刪除
Collection<Serializable> idList=new ArrayList<Serializable>();
idList.add(22);
idList.add(112);
userMapper.deleteBatchIds(idList);
//根據map集合進行刪除
Map<String,Object> columnMap=new HashMap<String,Object>();
columnMap.put("id",6);
columnMap.put("name","小明");
userMapper.deleteByMap(columnMap);
//根據wrapper進行刪除
QueryWrapper<User> wrapper=new QueryWrapper<>();
userMapper.delete(wrapper);
}
複製代碼
private static void updateUser(UserMapper userMapper){
//根據主鍵修改
userMapper.updateById(new User(112,"小滎滎","北京",new Date()));
UpdateWrapper<User> updateWrapper=new UpdateWrapper<>();
updateWrapper.eq("name","小滎滎");
updateWrapper.between("id",1,5);
userMapper.update(new User(112,"小滎","武漢",new Date()),updateWrapper);
}
複製代碼
測試的結果這裏就再也不進行展現了,你們能夠自行去測試app
源碼連接測試