ssm整合通用mapper
阿新 • • 發佈:2020-09-09
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 protected], 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 protected], created=Thu Jul 30 02:16:36 CST 2020, name=spring, status=1, qq=123456]