add cms-service

This commit is contained in:
shuzheng 2016-10-05 00:32:12 +08:00
parent 7147069772
commit 814aae9815
9 changed files with 344 additions and 0 deletions

50
cms/cms-service/pom.xml Normal file
View File

@ -0,0 +1,50 @@
<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">
<parent>
<artifactId>cms</artifactId>
<groupId>com.zheng</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cms-service</artifactId>
<packaging>jar</packaging>
<name>cms-service</name>
<url>http://www.zhangshuzheng.cn</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zheng</groupId>
<artifactId>cms-dao</artifactId>
<version>1.0.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<finalName>cms-service</finalName>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,25 @@
package com.zheng.mapper;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.zheng.model.User;
import com.zheng.model.UserVO;
/**
* 用户mapper
* @author shuzheng
* @date 2016年7月6日 下午6:05:54
*/
@Repository
public interface UserVOMapper extends UserMapper {
UserVO selectUserWithBook(int id);
List<User> selectAll(Map<String, Object> map);
void insertAutoKey(User user);
}

View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zheng.mapper.UserVOMapper">
<!-- 缓存 -->
<!--<cache type="PERPETUAL" eviction="LRU" flushInterval="60000" size="1024" readOnly="true"/>-->
<!-- 结果映射关系 -->
<resultMap id="UserResultMap" type="userVO">
<id column="u_id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="nickname" property="nickname"/>
<result column="sex" property="sex"/>
<result column="ctime" property="ctime"/>
<result column="content" property="content" />
<collection column="userid" property="books" ofType="Book">
<id column="b_id" property="id"/>
<result column="userid" property="userid"/>
<result column="name" property="name"/>
</collection>
</resultMap>
<!-- 查询一对多的所有记录 -->
<select id="selectUserWithBook" resultMap="UserResultMap" parameterType="int">
select
u.id u_id,u.username,u.password,u.nickname,u.sex,u.ctime,u.content,
b.id b_id,b.userid,b.name
from
user u
left join
book b
on
u.id=b.userid
where
u.id=#{id,jdbcType=INTEGER}
</select>
<!-- 查询所有记录 -->
<select id="selectAll" resultType="user" parameterType="java.util.Map">
select
<choose>
<when test="clumns != null">${clumns}</when>
<otherwise>
*
</otherwise>
</choose>
from
user
<where>
<if test="condition != null and condition != ''">${condition}</if>
</where>
order by
<choose>
<when test="order != null and order != ''">${order}</when>
<otherwise>id asc</otherwise>
</choose>
</select>
<!-- 插入单条记录插入前建好id索引 -->
<insert id="insertAutoKey" useGeneratedKeys="true" keyProperty="id" parameterType="user">
insert into
user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="username != null">username,</if>
<if test="password != null">password,</if>
<if test="nickname != null">nickname,</if>
<if test="sex != null">sex,</if>
<if test="ctime != null">ctime,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id,jdbcType=INTEGER},</if>
<if test="username != null">#{username,jdbcType=VARCHAR},</if>
<if test="password != null">#{password,jdbcType=VARCHAR},</if>
<if test="nickname != null">#{nickname,jdbcType=VARCHAR},</if>
<if test="sex != null">#{sex,jdbcType=INTEGER},</if>
<if test="ctime != null">#{ctime,jdbcType=BIGINT},</if>
</trim>
</insert>
</mapper>

View File

@ -0,0 +1,22 @@
package com.zheng.model;
import java.awt.print.Book;
import java.util.List;
/**
* 用户VO
* @author shuzheng
* @date 2016年7月6日 下午6:06:36
*/
public class UserVO extends User {
private List<Book> books;
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}

View File

@ -0,0 +1,16 @@
package com.zheng.service;
/**
* baseService接口
* @author shuzheng
* @date 2016年7月7日 上午9:58:23
*/
public interface BaseService<Mapper> {
/**
* 获取基本操作mapper
* @return
*/
Mapper getMapper();
}

View File

@ -0,0 +1,37 @@
package com.zheng.service;
import java.util.List;
import java.util.Map;
import com.zheng.mapper.UserMapper;
import com.zheng.model.User;
import com.zheng.model.UserVO;
/**
* 用户service接口
* @author shuzheng
* @date 2016年7月6日 下午6:03:45
*/
public interface UserService extends BaseService<UserMapper> {
/**
* 获取带book数据的用户
* @param id
* @return
*/
UserVO selectUserWithBook(int id);
/**
* 根据条件获取用户列表
* @param map
* @return
*/
List<User> selectAll(Map<String, Object> map);
/**
* 插入用户并返回主键
* @param user
*/
void insertAutoKey(User user);
}

View File

@ -0,0 +1,70 @@
package com.zheng.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zheng.mapper.UserMapper;
import com.zheng.mapper.UserVOMapper;
import com.zheng.model.User;
import com.zheng.model.UserVO;
import com.zheng.service.UserService;
/**
* 用户service实现
* @author shuzheng
* @date 2016年7月6日 下午6:07:58
*/
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private UserVOMapper userVOMapper;
/**
* 获取基本操作mapper
* @return
*/
@Override
public UserMapper getMapper() {
return userMapper;
}
/**
* 获取带book数据的用户
* @param id
* @return
*/
@Override
public UserVO selectUserWithBook(int id) {
return userVOMapper.selectUserWithBook(id);
}
/**
* 根据条件获取用户列表
* @param map
* @return
*/
@Override
public List<User> selectAll(Map<String, Object> map) {
return userVOMapper.selectAll(map);
}
/**
* 插入用户并返回主键
* @param user
*/
@Override
public void insertAutoKey(User user) {
userVOMapper.insertAutoKey(user);
}
}

View File

@ -0,0 +1,40 @@
package com.zheng.service;
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 org.springframework.test.context.transaction.TransactionConfiguration;
import com.zheng.mapper.UserMapper;
import com.zheng.model.User;
import com.zheng.model.UserVO;
/**
* 测试service
* @author shuzheng
* @date 2016年7月6日 下午6:07:43
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"classpath:applicationContext.xml",
"classpath:applicationContext-jdbc.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void index() {
// 自定义接口调用
UserVO userVO = userService.selectUserWithBook(1);
System.out.println(userVO.getBooks().size());
// 自动生成接口调用
User user2 = userService.getMapper().selectByPrimaryKey(1);
System.out.println(user2.getNickname());
}
}

View File

@ -16,5 +16,6 @@
<modules>
<module>cms-dao</module>
<module>cms-service</module>
</modules>
</project>