spring整合Mybatis-plus
今天就隨便說說spring整合mybatis-plus,就不再搭建一個web項目了,簡單做一個測試類。
既然是spring,那就少不了各種xxx.xml配置文件。
那就先說說配置文件
<1>. application-dao.xml
dao層的配置,他的核心就是要產生Mapper代理對象
1、數據源的配置
<context:property-placeholder
location="classpath:db.properties" system-properties-mode="FALLBACK" />
2、數據源的配置
<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>
3、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>
4、產生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.xml
<context:component-scan base-package="com.xieyunjie.service">
</context:component-scan>
<3>. applicationContext.xml
<!-- 引入dao層的配置 -->
<import resource="classpath:application-dao.xml"/>
<4>. db.properties
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.properties
# 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;
}
三、創建一個mapper接口(dao層)
public interface UserMapper extends BaseMapper<User> {
}
四、創建一個mapper.xml的映射文件
<?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對象,進行測試
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);
}
- 根據Id進行查詢
private static void query1(UserMapper userMapper){
User user=userMapper.selectById(3);
System.out.println(user);
}
- 將需要的數據放到一個Map集合裏面進行查詢
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);
}
- 將需要的數據放到一個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);
}
測試的結果這裏就不再進行展示了,大家可以自行去測試