1.在pom.xml中导入jar包(引入Maven依赖)
<!-- 通用mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.4</version> </dependency> |
2.在resources源文件中创建mybatis.xml配置通用mapper的拦截器
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <plugins> <!-- 通用mapper --> <plugin interceptor="com.github.abel533.mapperhelper.MapperInterceptor"> <!--主键自增回写方法,默认值MYSQL,详细说明请看文档 --> <property name="IDENTITY" value="MYSQL" /> <!--通用Mapper接口,多个通用接口用逗号隔开 --> <property name="mappers" value="com.github.abel533.mapper.Mapper" /> </plugin> </plugins> </configuration> |
3.在mybatis配置文件(applicationContent-mybatis.xml)中引入mybatis.xml
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 配置SqlSessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 数据源 --> <property name="dataSource" ref="dataSource"/> <!-- mapper映射文件 --> <property name="mapperLocations" value="classpath:com/zhiyou100/kfs/mapper/*.xml"/>
<!-- 引入mybatis文件 --> <property name="configLocation" value="classpath:mybatis.xml"/>
<!-- pagehelp拦截器 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置参数,一行配置一个 --> <value> reasonable=true </value> </property> </bean> </array> </property> </bean>
<!-- 接口生成实现类:用MapperScannerConfigurer使dao接口的包和SqlSessionFactory对象中的mapper对应 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sessionFactory"/> <property name="basePackage" value="com.zhiyou100.kfs.dao"/> </bean>
</beans> |
4.实体类要写相应的注解来对应数据库表和字段
package com.zhiyou100.kfs.bean;
import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;
@Table(name="tbl_user")//@Table:设置对应的表格;name:表格名; public class User { @Id//@Id:这个属性是主键 @GeneratedValue(strategy=GenerationType.IDENTITY)//设置MYSQL数据库主键递增(其他数据库有相应的字符串表示递增) private Integer userId; @Column(name="user_name")//这个属性userName和数据库user_name字段对应(属性名为userName会自动对应数据库中的user_name,驼峰命名大写会在数据库转化为下划线,若属性名转化后和数据字段名对应则@Column可以不写) private String userName; private Integer userAge; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public Integer getUserAge() { return userAge; } public void setUserAge(Integer userAge) { this.userAge = userAge; } @Override public String toString() { return "User [userId=" userId ", userName=" userName ", userAge=" userAge "]"; } } |
5.dao接口继承通用mapper的接口(mapper<T>,T表示泛型,输入什么就变成什么),这个接口必须写,后面可以加映射文件(如UserMapper.xml)和其他的方法,但映射文件的方法的id不能和通用mapper方法的方法名一致
package com.zhiyou100.kfs.dao;
import com.github.abel533.mapper.Mapper; import com.zhiyou100.kfs.bean.User;
public interface UserMapper extends Mapper<User>{
} |