项目结构
1.UserController &&&DeptController
package com.zr.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.zr.entity.User;
import com.zr.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//模糊查询
@ResponseBody
@RequestMapping("/findLikeId.action")
public String findLikeId(String userid) {
List<User> user = userService.findLikeId(userid);
return JSON.toJSONString(user);
}
//登录
@ResponseBody
@RequestMapping("/login.action")
public String login(String userid,String password) {
User user = userService.login(userid,password);
return JSON.toJSONString(user);
}
// map登录
@ResponseBody
@RequestMapping("/findByidAndpwd.action")
public String findByidAndpwd(String userid,String password) {
User user = userService.findByidAndpwd(userid,password);
return JSON.toJSONString(user);
}
//统计一共有多少条数据
@ResponseBody
@RequestMapping("/count.action")
public String count() {
int count = userService.count();
return "{\"count\":" + count + "}";
//return String.valueOf(count);
}
//order 排列
@ResponseBody
@RequestMapping("/order.action")
public String order(String column,String order) {
List<User> user = userService.order(column,order);
return JSON.toJSONString(user);
}
//一对1
@ResponseBody
@RequestMapping("/findone.action")
public String findone(String userid) {
User user = userService.findone(userid);
return JSON.toJSONString(user);
}
}
**//deptController**
package com.zr.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zr.entity.Dept;
import com.zr.entity.User;
import com.zr.service.DeptService;
import com.zr.util.MsgUtil;
@Controller
@RequestMapping("/dept")
public class DeptController {
private DeptService deptService;
//1duiduo
@ResponseBody
@RequestMapping("/findDept.action")
public String findDept(String id) {
Dept dept = deptService.findDept(id);
return JSON.toJSONString(dept);
}
@ResponseBody
@RequestMapping("/add.action")
public String add() {
MsgUtil msg=new MsgUtil();
try {
deptService.add();
msg.setSuccess(true);
msg.setMessage("新增成功");
} catch (Exception e) {
msg.setSuccess(false);
msg.setMessage("失败");
}
return JSON.toJSONString(msg);
}
}
2.UserService&&&DeptService
package com.zr.service;
import java.util.List;
import com.zr.entity.User;
public interface UserService {
List<User> findLikeId(String userid);
public User login(String userid, String password);
public int count();
public User findByidAndpwd(String userid, String password);
List<User> order(String column, String order);
User findone(String userid);
}
**$$$$$$$$$$$$$$$$$$$$DeptService**
package com.zr.service;
import com.zr.entity.Dept;
public interface DeptService {
Dept findDept(String id);
void add();
}
3.UserServiceImp&&&DeptServiceImpl
package com.zr.serviceimpl;
import java.util.HashMap;
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.zr.entity.User;
import com.zr.mapper.UserMapper;
import com.zr.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
public List<User> findLikeId(String userid) {
// TODO Auto-generated method stub
return userMapper.findLikeId(userid);
}
public User login(String userid, String password) {
// TODO Auto-generated method stub
return userMapper.login(userid,password);
}
public int count() {
// TODO Auto-generated method stub
return userMapper.count();
}
public User findByidAndpwd(String userid, String password) {
// TODO Auto-generated method stub
Map<String,Object> paramMap=new HashMap<String,Object>();
paramMap.put("password",password);
return userMapper.findByidAndpwd(paramMap);
}
public List<User> order(String column, String order) {
// TODO Auto-generated method stub
return userMapper.order(column, order);
}
public User findone(String userid) {
// TODO Auto-generated method stub
return userMapper.findone(userid);
}
}
**&&&&&&&&&&&&&&DeptServiceImpl**
package com.zr.serviceimpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zr.entity.Dept;
import com.zr.mapper.DeptMapper;
import com.zr.service.DeptService;
@Service
@Transactional
public class DeptServiceImpl implements DeptService {
private DeptMapper deptmapper;
public Dept findDept(String id) {
// TODO Auto-generated method stub
return deptmapper.findDept(id);
}
public void add() {
Dept d1=new Dept();
d1.setId("30");
d1.setName("运营部");
d1.setTel("900");
deptmapper.insert(d1);
Dept d2=new Dept();
d1.setId("30");
d1.setName("运营部");
d1.setTel("900");
deptmapper.insert(d2);
}
}
4.UserMapper &&&DeptMapper
package com.zr.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.zr.entity.User;
public interface UserMapper {
int deleteByPrimaryKey(String userid);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(String userid);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> findLikeId(String userid);
User login(@Param("id")String userid, @Param("pwd")String password);
int count();
User findByidAndpwd(Map<String, Object> paramMap);
//List<User> order(@Param("String column")String column,@Param("String order") String order);
List<User> order(@Param("column")String column,@Param("order")String order);
User findone(String userid);
}
**$$$$$$$$$$$$$DeptMapper**
package com.zr.mapper;
import com.zr.entity.Dept;
public interface DeptMapper {
int deleteByPrimaryKey(String id);
int insert(Dept record);
int insertSelective(Dept record);
Dept selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(Dept record);
int updateByPrimaryKey(Dept record);
Dept findDept(String id);
int add();
}
5.UserMapper.xml&&DeptMapper.xml
<?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.zr.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.zr.entity.User">
<id column="userid" jdbcType="VARCHAR" property="userid" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="tel" jdbcType="VARCHAR" property="tel" />
<result column="deptid" jdbcType="VARCHAR" property="deptid" />
</resultMap>
<sql id="Base_Column_List">
userid, username, password, tel, deptid
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
where userid = #{userid,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from user
where userid = #{userid,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.zr.entity.User">
insert into user (userid, username, password,
tel, deptid)
values (#{userid,jdbcType=VARCHAR}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{tel,jdbcType=VARCHAR}, #{deptid,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.zr.entity.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userid != null">
userid,
</if>
<if test="username != null">
username,
</if>
<if test="password != null">
password,
</if>
<if test="tel != null">
tel,
</if>
<if test="deptid != null">
deptid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userid != null">
#{userid,jdbcType=VARCHAR},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
#{password,jdbcType=VARCHAR},
</if>
<if test="tel != null">
#{tel,jdbcType=VARCHAR},
</if>
<if test="deptid != null">
#{deptid,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zr.entity.User">
update user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null">
password = #{password,jdbcType=VARCHAR},
</if>
<if test="tel != null">
tel = #{tel,jdbcType=VARCHAR},
</if>
<if test="deptid != null">
deptid = #{deptid,jdbcType=VARCHAR},
</if>
</set>
where userid = #{userid,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.zr.entity.User">
update user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
tel = #{tel,jdbcType=VARCHAR},
deptid = #{deptid,jdbcType=VARCHAR}
where userid = #{userid,jdbcType=VARCHAR}
</update>
<!-- 模糊查询 -->
<select id="findLikeId" parameterType="java.lang.String" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"></include>
from user
where userid like concat('%',#{userid},'%')
</select>
<!-- 登录 -->
<select id="login" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"></include>
from user
where userid=#{id} and password=#{pwd}
</select>
<!-- 多条数据 -->
<select id="findByidAndpwd" parameterType="java.util.Map" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"></include>
from user
where userid=#{userid} and password=#{password}
</select>
<!-- 总共多少条数据-->
<select id="count" resultType="java.lang.Integer" >
select count(*) from user
</select>
<!--1对1 -->
<resultMap id="oneMap" type="com.zr.entity.User">
<id column="userid" jdbcType="VARCHAR" property="userid" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="tel" jdbcType="VARCHAR" property="tel" />
<result column="deptid" jdbcType="VARCHAR" property="deptid" />
<association property="dept" javaType="com.zr.entity.Dept">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="depttel" jdbcType="VARCHAR" property="tel" />
</association>
</resultMap>
<select id="findone" resultType="java.lang.String" resultMap="oneMap" >
select u.*,d.id,d.name,d.tel as depttel
from user u LEFT JOIN dept d on u.deptid=d.id where u.userid=#{userid}
</select>
<!-- 动态排序 因为有双引号占位符,所以用EL表达式-->
<select id="order" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List"></include>
from user
order by
${column} ${order}
</select>
</mapper>
**$$$$$$$$$$$$$$$$$$$$$$$$$$$$DeptMapper.xml**
<?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.zr.mapper.DeptMapper">
<resultMap id="BaseResultMap" type="com.zr.entity.Dept">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="tel" jdbcType="VARCHAR" property="tel" />
</resultMap>
<sql id="Base_Column_List">
id, name, tel
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from dept
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
delete from dept
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.zr.entity.Dept">
insert into dept (id, name, tel
)
values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{tel,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.zr.entity.Dept">
insert into dept
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="tel != null">
tel,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="tel != null">
#{tel,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.zr.entity.Dept">
update dept
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="tel != null">
tel = #{tel,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.zr.entity.Dept">
update dept
set name = #{name,jdbcType=VARCHAR},
tel = #{tel,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<!-- 1对多 oftype sql语句多的在前面 千万别忘记在实体类中加 List<User> users 重复类名-->
<resultMap id="moreMap" type="com.zr.entity.Dept">
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="tel" jdbcType="VARCHAR" property="tel" />
<collection property="users" ofType="com.zr.entity.User">
<id column="userid" jdbcType="VARCHAR" property="userid" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="usertel" jdbcType="VARCHAR" property="tel" />
<result column="deptid" jdbcType="VARCHAR" property="deptid" />
</collection>
</resultMap>
<select id="findDept" resultType="java.lang.String" resultMap="moreMap" >
select d.*,u.userid,u.username,u.password,u.tel as usertel
from dept d LEFT JOIN user u on u.deptid=d.id where id=#{id}
</select>
</mapper>
6.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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zr</groupId>
<artifactId>ssm528</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssm528 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<servlet.version>4.0.1</servlet.version>
<spring.version>5.2.5.RELEASE</spring.version>
<mybatis.version>3.5.4</mybatis.version>
<mybatis-spring.version>2.0.4</mybatis-spring.version>
<mysql.version>5.1.48</mysql.version>
<druid.version>1.1.22</druid.version>
<jackson.version>2.10.3</jackson.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>9</java.version>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.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.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>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
<build>
<finalName>ssm528</finalName>
</build>
</project>
7.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mybatis.xml</param-value>
</context-param> -->
<!-- <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping> -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml,classpath:mybatis.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
</web-app>
8.config.propertites
driver=com.mysql.jdbc.Driver
#mysql://本机地址:mysql端口号(默认是3306)/数据库名称?编码
url=jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=UTF-8
username=root
password=mysql
initialSize=10
maxActive=1000
maxWait=60000
9.mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/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">
<!-- 自动扫描 事物注解不好使 mybatis扫描service mapper-->
<context:component-scan base-package="com.zr.service,com.zr.mapper" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config.properties" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/zr/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zr.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启事务驱动注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
10.spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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">
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.zr.controller"></context:component-scan>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
11.MsgUtil.java
package com.zr.util;
public class MsgUtil {
private boolean success;
private String message;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
12.数据库 Dept和User
dept
user