相关jar包下载地址 https://mapperhelper.github.io/docs/1.integration/
mybatis通用mapper
mapper-4.1.5.jar persistence-api-1.0.jar (通用mapper依赖persistence-api-1.0.jar)
spring版本
spring-aop-4.1.2.RELEASE.jar
spring-beans-4.1.2.RELEASE.jar
spring-context-4.1.2.RELEASE.jar
spring-core-4.1.2.RELEASE.jar
spring-expression-4.1.2.RELEASE.jar
spring-jdbc-4.1.2.RELEASE.jar
spring-test-4.1.2.RELEASE.jar
spring-tx-4.1.2.RELEASE.jar
spring-web-4.1.2.RELEASE.jar
spring-webmvc-4.1.2.RELEASE.jar
数据库相关
mysql-connector-java-5.1.21.jar
druid-1.0.15.jar
Mybatis框架
mybatis-3.3.0.jar
mybatis-spring-1.1.1.jar
其它依赖
lombok-1.16.6.jar
log4j-1.2.14.jar hamcrest-core-1.3.jar
commons-logging-1.2.jar
commons-io-2.5.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
数据库配置 db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis jdbc.username=root jdbc.password=root
mybatis.cfg.xml
<?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>
<settings>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="false"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode"/>
</settings>
</configuration>
applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" 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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 1.导入数据库配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 2.开启@Autowired注解 --> <context:annotation-config/> <!-- 3.扫描Spring Bean组件 --> <context:component-scan base-package="com.gzcgxt"/> <!-- 4.jdbcTemplate数据库操作组件 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="dataSource"/> </bean> <!-- 5.Druid数据源配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.gzcgxt.mapper" /> <property name="properties"> <value> mappers=tk.mybatis.mapper.common.Mapper </value> </property> </bean> <!--4:配置sesssionFactory--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> <property name="mapperLocations" value="classpath:com/gzcgxt/mapper/*.xml"/> <property name="typeAliasesPackage" value="com.gzcgxt.erp.domain"/> </bean> </beans>
User.java
package com.gzcgxt.domain; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import lombok.Data; @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private Integer age; private Integer flag; }
UserMapper.java
package com.gzcgxt.mapper; import tk.mybatis.mapper.common.Mapper; import com.gzcgxt.domain.User; public interface UserMapper extends Mapper<User>{ }
测试代码
package com.gzcgxt; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.gzcgxt.domain.User; import com.gzcgxt.mapper.UserMapper; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class APP { @Autowired private UserMapper userMapper; @Test public void testMysql() { User u = userMapper.selectByPrimaryKey(6); System.out.println(u); } }