Spring+SpringMVC+MybatisPlus整合
MybatisPlus是mybatis的升级版,Mybatis-Plus不会对现有的 Mybatis 构架产生任何影响,更容易上手,功能更强大!。
接上小节案例,实现Spring+SpringMVC+Mybatisplus整合。
实现步骤与上节几乎完全相同,仅仅是applicationContext.xml,和service层的接口和实现不同而已。
下面给出不同之处。
首先在pom.xml中添加mybatis-plus的依赖。
<properties>
...
<mybatis-plus.version>3.3.2</mybatis-plus.version>
<jpa.version>2.2</jpa.version>
<hibernate.version>5.4.32.Final</hibernate.version>
</properties>
<!--不要忘记添加spring-orm组件-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${jpa.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<?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:mvc="http://www.springframework.org/schema/mvc" 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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
<mvc:annotation-driven/>
<!-- 访问静态资源 -->
<mvc:default-servlet-handler/>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 使用扫描机制扫描控制器类,控制器类都在controller包及其子包下 -->
<context:component-scan base-package="com"/>
<!--创建sqlSessionFactory对象-->
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<bean id="dataSource" class="${mysql.dataSource}">
<property name="Username" value="${mysql.username}"/>
<property name="Password" value="${mysql.password}"/>
<property name="DriverClassName" value="${mysql.driver}"/>
<property name="Url" value="${mysql.url}"/>
</bean>
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.entity"/>
<property name="configuration">
<bean class="com.baomidou.mybatisplus.core.MybatisConfiguration">
<property name="autoMappingBehavior" value="FULL"/>
<property name="mapUnderscoreToCamelCase" value="true"/>
</bean>
</property>
<!--分页插件-->
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dbType" value="MYSQL"/>
</bean>
</array>
</property>
</bean>
<!--递归扫描dao成接口,进行动态映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mapper"/>
</bean>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/html/"/>
<!--后缀-->
<property name="suffix" value=".html"/>
</bean>
<!--spring整合jpa配置-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.entity" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true"/>
<property name="showSql" value="true" />
</bean>
</property>
</bean>
</beans>
2.实体类
package com.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("users")
@Builder
@Entity
@Table(name="users")
public class Users implements Serializable, Cloneable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@TableId(type=IdType.AUTO) //等同于MySQL的auto_increment
private Integer uid;
private String username;
private String password;
private String email; //电子邮箱
private String gender; //性别
private int education; //学历
private String birthday; //生日
@TableField(exist = false)
@Transient
private String[] favorites; //爱好;这个无法映射为数据库的一个字段
private String introduce; //自我介绍
private String province; //籍贯
@TableField(exist = false)
@Transient
private String accept; //是否接受协议
}
3.Service(服务层)
UsersService.java
package com.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.entity.Users;
public interface UsersService extends IService<Users> {
public boolean reg(Users user) throws Exception;
public Users login(String username,String password);
}
UsersServiceImpl.java
package com.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entity.Users;
import com.mapper.UsersMapper;
import com.service.UsersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper,Users> implements UsersService {
@Resource
private UsersMapper usersMapper;
@Transactional(rollbackFor =Exception.class)
@Override
public boolean reg(Users user) throws Exception {
return super.save(user);
}
@Override
public Users login(String username, String password) {
QueryWrapper<Users> queryWrapper = new QueryWrapper<Users>();
queryWrapper.eq("username",username);
queryWrapper.eq("password",password);
return super.getOne(queryWrapper);
}
}
package com.service;
import com.entity.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class UsersServiceTest {
@Resource
private ApplicationContext context;
@Resource
private UsersService usersService;
@Test
public void testLogin() {
Users u = usersService.login("zhangsan", "123456");
System.out.println(u);
Assert.notNull(u,"");
}
@Test
public void testReg() throws Exception{
Users u = Users.builder().username("messi").password("123456").gender("男").email("454454566@qq.com").birthday("2000-10-10").province("阿根廷").education(8).introduce("球王").build();
boolean flag = usersService.reg(u);
Assert.isTrue(flag,"");
}
}