1.1使用parent工程控制版本
1.1.1创建Maven工程的pom工程qhrgd-parent来控制项目jar的版本.
1.1.2 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.qhrgd</groupId> <artifactId>qhrgd-parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging>
<properties> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.7</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.32</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <jolbox.version>0.8.0.RELEASE</jolbox.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.7.5</pagehelper.version> <mapper.version>2.3.4</mapper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <commons-codec.version>1.9</commons-codec.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> <dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.7</zookeeper.version> <zkclient.version>0.1</zkclient.version> <activemq.version>5.12.0</activemq.version> <freemarker.version>2.3.23</freemarker.version> <quartz.version>2.2.2</quartz.version> <uediter.version>1.1.1</uediter.version> <json.version>20160212</json.version> <fastdfs_client.version>1.25</fastdfs_client.version> </properties>
<dependencyManagement> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency>
<!-- Spring --> <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-context-support</artifactId> <version>${spring.version}</version> </dependency>
<!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency>
<!-- 通用Mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency>
<!-- 分页助手 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <dependency> <groupId>com.github.jsqlparser</groupId> <artifactId>jsqlparser</artifactId> <version>${jsqlparser.version}</version> </dependency>
<!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency>
<!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency>
<!-- Jackson Json处理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency>
<!-- 连接池 --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>${jolbox.version}</version> </dependency>
<!-- JSP相关 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency>
<!-- 时间操作组件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency>
<!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency>
<!-- 文件上传组件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency>
<!-- dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency>
<!-- 加密解密 --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec.version}</version> </dependency>
<!-- 定时任务Quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
<!-- ActiveMQ依赖 --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency>
<!-- 静态化freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency>
<!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency>
<!-- solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency>
<!-- 百度编辑器 --> <dependency> <groupId>com.baidu</groupId> <artifactId>ueditor</artifactId> <version>${uediter.version}</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>${json.version}</version> </dependency>
<dependency> <groupId>com.alibaba.fastdfs</groupId> <artifactId>fastdfs_client</artifactId> <version>${fastdfs_client.version}</version> </dependency>
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> </dependencies>
</dependencyManagement>
<build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 资源文件拷贝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> <resources> <!-- 使用Maven部署的时候,xml和properties配置文件也一起部署到Tomcat --> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <!-- 默认是以下配置 --> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project> |
1.2 创建通用mapper工程
1.2.1 创建如图所示的Maven工程
1.2.2 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> <parent> <groupId>com.qhrgd</groupId> <artifactId>qhrgd-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>qhrgd-mybatis-mapper</groupId> <artifactId>qhrgd-mybatis-mapper</artifactId> <version>0.0.1-SNAPSHOT</version>
<dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- 通用mapper --> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency>
<!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- Apache工具组件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> </dependency>
</dependencies> </project> |
1.2.3 使用通用Mapper实现CRUD
1.2.3.1 配置通用mepper插件
在resource目录下,创建mybatis-config.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> <!-- 在同一层目录下,扫描数据库的配置文件 --> <properties resource="jdbc.properties" />
<!-- 配置通用Mapper插件 --> <plugins> <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> <!-- 获取数据源 --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <!-- 扫描配置完成的通用mapper --> <mappers> <!-- 基于XML的实现 <mapper resource="UserMapper.xml" />--> <!-- 引入单个通用接口 <mapper class="com.qhr.mybatis.mapper.NewUserMapper"/>--> <!-- 引用多个通用接口 --> <package name="com.qhr.mapper"/> </mappers> </configuration> |
数据库的配置文件jdbc.properties的配置,如下:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis-mapper?useUnicode=true&characterEncoding=utf8&autoReconnect=true jdbc.username=root jdbc.password=123456 |
1.2.3.2创建pojo进行数据库的映射
package com.qhr.pojo;
import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; //表名 @Table(name="user") public class User { //主键 @Id //自增长 @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; //字段,倘若括号里面什么都没写,数据相应的要以username为字段名 @Column(name="username") private String username; @Column(name="password") private String password; @Column(name="phone") private String phone; @Column(name="email") private String email; 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; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", phone=" + phone + ", email=" + email + "]"; }
}
|
1.2.3.3 创建通用mapper的接口
package com.qhr.mapper;
import com.github.abel533.mapper.Mapper; import com.qhr.pojo.User;
public interface UserMapper extends Mapper<User>{
}
|
1.2.3.4 完成的目录结构
1.3 通用Mapper的测试
创建NewUserMapper测试类,如下:
package com.qhrgd;
import java.io.IOException; import java.io.InputStream; import java.util.List;
import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test;
import com.github.abel533.entity.Example; import com.qhr.mapper.UserMapper; import com.qhr.pojo.User;
public class NewUserMapperTest { private SqlSessionFactory sqlSessionFactory;
private SqlSession sqlSession;
private UserMapper newUserMapper;
@Before public void init() { String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession(true);
newUserMapper = sqlSession.getMapper(UserMapper.class);
} /** * 根据条件查询相应的用户,返回值为用户实体,即User */ @Test public void testSelectOne() { User user = new User(); user.setId(1L); User user2 = this.newUserMapper.selectOne(user); System.out.println(user2); }
/** * 根据条件查询相应的用户集合,返回值为用户集合,即List<User> */ @Test public void testSelect() {
List<User> list = this.newUserMapper.select(null);
for (User user2 : list) { System.out.println(user2); } } /** * 根据条件查询符合条件的用户实体数量,返回值为int */ @Test public void testSelectCount() { User user = new User(); user.setUsername("zhangsan"); int count = this.newUserMapper.selectCount(null); System.out.println(count); } /** * 根据主键查询特定用户,返回值类型为User */ @Test public void testSelectByPrimaryKey() { User user = new User(); user.setId(1L); User user2 = this.newUserMapper.selectByPrimaryKey(user); System.out.println(user2); } /** * 插入特定用户 */ @Test public void testInsert() { User user = new User(); user.setId(null); user.setUsername("张三丰2"); user.setPassword("12345123"); this.newUserMapper.insert(user); }
@Test public void testInsertSelective() { User user = new User(); user.setId(null); user.setUsername("张2"); user.setPassword("123"); this.newUserMapper.insertSelective(user); } /** * 根据条件删除用户信息 */ @Test public void testDelete() { User user = new User(); user.setUsername("张2"); user.setPassword("123"); this.newUserMapper.delete(user); } /** * 根据主键删除用户信息 */ @Test public void testDeleteByPrimaryKey() { User user = new User(); user.setId(1L); this.newUserMapper.deleteByPrimaryKey(user); } /** * 根据主键修改用户信息 */ @Test public void testUpdateByPrimaryKey() { User user = new User(); user.setId(12L); user.setUsername("张三2"); user.setPassword("123123"); this.newUserMapper.updateByPrimaryKey(user); sqlSession.commit(); } /** * 根据主键修改用户信息 */ @Test public void testUpdateByPrimaryKeySelective() { User user = new User(); user.setId(12L); user.setUsername("张三"); user.setPassword("3333333"); this.newUserMapper.updateByPrimaryKeySelective(user); sqlSession.commit(); }
/** * 查询1到10主键的用户实体数量,返回值为int */ @Test public void testSelectCountByExample() { Example example = new Example(User.class); example.createCriteria().andBetween("id", 1L, 10L); int countByExample = this.newUserMapper.selectCountByExample(example); System.out.println(countByExample); } /** * 删除10到12为主键的用户信息,返回值为int */ @Test public void testDeleteByExample() { Example example = new Example(User.class); example.createCriteria().andBetween("id", 10L, 12L); int countByExample = this.newUserMapper.deleteByExample(example); System.out.println(countByExample); } /** * 查询1到10的用户信息,返回值为用户集合List<User> */ @Test public void testSelectByExample() { Example example = new Example(User.class); example.createCriteria().andBetween("id", 1L, 10L); List<User> list = this.newUserMapper.selectByExample(example); for (User user : list) { System.out.println(user); } } /** * */ @Test public void testUpdateByExampleSelective() { Example example = new Example(User.class); example.createCriteria().andEqualTo("username", "asdf"); User user = new User(); user.setUsername("test"); int count = this.newUserMapper.updateByExampleSelective(user,example); } /** * */ @Test public void testUpdateByExample() { Example example = new Example(User.class); example.createCriteria().andEqualTo("username", "张三丰"); User user = new User(); user.setUsername("asdf"); user.setPassword("asdfasdf"); int count = this.newUserMapper.updateByExample(user,example); } }
|