ssm整合通用mapper

1.加入通用mapper依赖

          <!--通用Mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>4.1.4</version>
		</dependency>

  

完整的pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.qingfeng</groupId>
	<artifactId>SSM-Mapper</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<properties>
		<spring.version>5.0.5.RELEASE</spring.version>
		<mybatis.version>3.4.5</mybatis.version>
		<mybatis.spring.version>1.3.1</mybatis.spring.version>
		<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
		<junit.version>4.12</junit.version>
		<jackson.version>2.9.4</jackson.version>
		<druid.version>1.1.0</druid.version>
		<mysql-connector-java>5.1.30</mysql-connector-java>
		<spring.security.version>5.0.5.RELEASE</spring.security.version>
	</properties>

	<dependencies>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>

		<!-- 单元测试 -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>

		<!-- Mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- MyBatis整合Spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis.spring.version}</version>
		</dependency>
		<!-- Mybatis的分页插件 -->
		<dependency>
			<groupId>com.github.miemiedev</groupId>
			<artifactId>mybatis-paginator</artifactId>
			<version>${mybatis.paginator.version}</version>
		</dependency>

		<!-- jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>${jackson.version}</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>${jackson.version}</version>
		</dependency>


		<!--通用Mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>4.1.4</version>
		</dependency>


		<!-- 数据源/数据库连接池 -->
		<!-- druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid.version}</version>
		</dependency>

		<!-- MySQL数据库连接驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector-java}</version>
		</dependency>

		<!-- lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.12</version>
		</dependency>

		<!--引入Servlet支持 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>


	</dependencies>



	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<!-- 指定端口 -->
					<port>9002</port>
					<!-- 请求路径 -->
					<path>/</path>
				</configuration>
			</plugin>
		</plugins>
	</build>


</project>

  

和通用 Mapper 以前版本一样,可以直接使用 tk.mybatis 提供的  tk.mybatis.spring.mapper.MapperScannerConfigurer  进行配置,这个配置和
MyBatis 官方提供的  org.mybatis.spring.mapper.MapperScannerConfigurer  区别只是第一层的包名, tk  和  org 。所以使用这种方式时,如果你项目已经使用  org.  进行了配置,只需要改成  tk.  即可。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 指定数据源,值为以上配置的数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!--指定mapper.xml映射文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml" /> 
    </bean>

  

完整的

<?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: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.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>


    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.driverClassName}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="initialSize" value="3" />
        <property name="minIdle" value="3" />
        <property name="maxActive" value="20" />
        <property name="maxWait" value="60000" />
        <property name="filters" value="stat,wall" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载mybatis的配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 指定数据源,值为以上配置的数据源 -->
        <property name="dataSource" ref="dataSource" />
        <!--指定mapper.xml映射文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*.xml" /> 
    </bean>

    <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定接口文件的位置 -->
        <property name="basePackage" value="com.qingfeng.dao" />
        <!--指定sqlSessionFactory,值为以上配置的sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- tx事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 启动注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- druid -->
    <!--监控数据相关的-->
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <!--当时间超过3000毫秒,这就是个慢sql,需要我们去处理-->
        <property name="slowSqlMillis" value="3000" />
        <!--发现了慢SQL,可以以log的日志计录下来-->
        <property name="logSlowSql" value="true" />
        <property name="mergeSql" value="true" />
    </bean>

    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <!--指定监控的db是mysql-->
        <property name="dbType" value="mysql" />
    </bean>

</beans>

  

实体类映射

1.表名可以使用@Table(name = "tableName")进行指定,对不符合第一条默认规则的可以通过这种方式指定表名.

2.字段默认和@Column一样,都会作为表字段,表字段默认为Java对象的Field名字驼峰转下划线形式.

3.可以使用@Column(name = "fieldName")指定不符合第3条规则的字段名

4.使用@Transient注解可以忽略字段,添加该注解的字段不会作为表字段使用.

5.建议一定是有一个@Id注解作为主键的字段,可以有多个@Id注解的字段作为联合主键.

6.默认情况下,实体类中如果不存在包含@Id注解的字段,所有的字段都会作为主键字段进行使用(这种效率极低).

7.实体类可以继承使用,可以参考测试代码中的com.github.abel533.model.UserLogin2类.

8.由于基本类型,如int作为实体类字段时会有默认值0,而且无法消除,所以实体类中建议不要使用基本类型.

  

package com.qingfeng.pojo;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Id;
import javax.persistence.Table;
/**
 * 用户实体类
 * @author Administrator
 *
 */
@Table(name="tb_user")
public class TBUser implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1223633684656774146L;

	@Id
	private Long id;//id

	private String username;//用户名

	private String password;//密码,加密存储

	private String phone;//注册手机号

	private String email;//注册邮箱

	private Date created;//创建时间

	private String name;//真实姓名

	private String status;//使用状态(1正常 0非正常)

	private String qq;//QQ号码

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + ", email="
				+ email + ", created=" + created + ", name=" + name + ", status=" + status + ", qq=" + qq + "]";
	}

	

}

  

创建Mapper接口

package com.qingfeng.dao;

import com.qingfeng.pojo.TBUser;

import tk.mybatis.mapper.common.Mapper;
/**
 * 继承通用Mapper
 * @author Administrator
 *
 */
public interface UserMapper extends Mapper<TBUser> {

}

  这里继承了   tk.mybatis.mapper.common.Mapper  接口,在接口上指定了泛型类型  Brand 。当你继承了  Mapper  接口后,此时就已经有了针对  Brand  的大量方法

 

测试:

package com.qingfeng;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qingfeng.dao.UserMapper;
import com.qingfeng.pojo.TBUser;



public class TestUserMapper {

	private AbstractApplicationContext ac;
	private UserMapper userMapper; 

	
	
	@Before
	public void doBefore() {
		
		ac = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
		userMapper = ac.getBean("userMapper",
				UserMapper.class);
	}
	
	@After
	public void doAfter() {
		ac.close();
	}
	
	
	@Test
	public void testUser() {
		List<TBUser> selectAll = userMapper.selectAll();
		for(TBUser user:selectAll) {
			System.out.println(user);
		}
	}
}   

  结果:

User [id=1, username=admin, password=123456, phone=13699996666, email=1412330@qq.com, created=Thu Jul 30 23:23:29 CST 2020, name=qq, status=1, qq=1412330]
User [id=2, username=spring, password=$2a$10$rIxa8dDL8F8Bf.TeC5rOeev96e0wTo0FIuLmtdJ6T/a8CptHlAlga, phone=13698566966, email=123456@qq.com, created=Thu Jul 30 02:16:36 CST 2020, name=spring, status=1, qq=123456]

  

 

posted @ 2020-09-09 20:53  Amy清风  阅读(594)  评论(0编辑  收藏  举报