SSM框架的搭建可以参考前一篇博文,里面有详细步骤。---传送门---

项目结构如图所示:

下面开始上源码

applicationContext:

 

<?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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com.cn" />
    <!--导入配置文件-->
    <bean id="mysqlSource" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbcConfig.properties"></property>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${classDriverName}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <!--spring mybatis的整合-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:*.xml" />
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cn.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

springmvcConfig:

 

 

<?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"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
 ">
	
	<context:annotation-config />
	<context:component-scan base-package="com.cn.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>
	<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean
				class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	<mvc:default-servlet-handler />

</beans>

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>body</display-name>
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>
	<error-page>
		<error-code>404</error-code>
		<location>/errorpage/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/errorpage/500.jsp</location>
	</error-page>
	<filter>
		<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
<!--  	<filter>
		<filter-name>SecurityServlet</filter-name>
		<filter-class>com.cn.fillter.SecurityServlet</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>SecurityServlet</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/applicationContext.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpass:log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>WEB-INF/springmvcConfig.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>default</servlet-name>
		<url-pattern>*.css</url-pattern>
	</servlet-mapping>

</web-app>

jdbc:

 

 

url=jdbc:mysql://localhost:3306/bodybuilding?useUnicode=true&characterEncoding=utf-8
username=root
password=root


classDriverName=com.mysql.jdbc.Driver
minPoolSize=1
maxPoolSize=300
maxIdleTime=60
acquireIncrement=5
initialPoolSize=1
idleConnectionTestPeriod=60U

UserController

 

 

package com.cn.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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.cn.pojo.User;
import com.cn.service.IUserService;

@Controller  
@RequestMapping("/user")  
public class UserController {
	@Resource  
    private IUserService userService;  
      
	/**
	 * 登录
	 * @return
	 */
	@ResponseBody
    @RequestMapping("/login")  
    public String toIndex(User user,HttpSession session){  
		String account=user.getAccount();
		user=userService.userLogin(user.getAccount(),user.getPwd());
		if(user!=null){
			List<Map<String,Object>> list = userService.findUserByAccount(account);
			session.setAttribute("userInfo", JSON.toJSON(list));
			session.setAttribute("account", account);
			return "true";
		}
		else{
			return "false";
		}
    }  
	/**
	 * 登录跳转
	 * @return
	 */
    @RequestMapping("/Forward")  
    public String loginForward(){  
		return "user/index";
    }  
    /**
     * 退出
     * @return
     */
    @RequestMapping("/exit")  
    public String exit(HttpSession session){  
    	session.invalidate();
    	return "login";
    }  
    /**
     * 个人信息
     * @return
     */
    @RequestMapping("/myInfo")  
    public String myInfo(HttpSession session){  
    	String account=(String) session.getAttribute("account");
    	List<Map<String,Object>> list = userService.findUserByAccount(account);
		session.setAttribute("userInfo", JSON.toJSON(list));
    	return "user/index";
    }  
    /**
     * 课程信息
     * @return
     */
    @RequestMapping("/classTable")  
    public String classTable(HttpSession session){  
    	List<Map<String,Object>> list = userService.findClass();
		session.setAttribute("classInfo", JSON.toJSON(list));
    	return "user/classTable";
    }  
    /**
     * 教练信息
     * @return
     */
    @RequestMapping("/teachList")  
    public String teachList(HttpSession session){  
    	return "user/teachList";
    }  
    /**
     * 查看教练
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectTeach")  
    public String selectTeach(HttpSession session,Integer id){  
    	List<Map<String,Object>> list = userService.findSeTeach(id);
		session.setAttribute("SelectTeachInfo", JSON.toJSON(list));
		return "true";
    }  
    /**
     * 选择教练
     * @return
     */
    @ResponseBody
    @RequestMapping("/chooseTeach")  
    public String chooseTeach(Integer id,Integer cid,Integer uid){  
    	userService.chooseTeach(id,cid,uid);
    	return "true";
    }  
    /**
     * 更新个人信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateUserInfo")  
    public String updateUserInfo(Integer id,String account,Integer sex,Integer age,String name,String pwd,String tel,String address){  
    	userService.updateUserInfo(id,account,sex,age,name,pwd,tel,address);
    	return "true";
    }  
}

SysController

 

 

package com.cn.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.pojo.Admin;
import com.cn.pojo.Teach;
import com.cn.pojo.User;
import com.cn.service.IUserService;

@Controller  
@RequestMapping("/sys")  
public class SysController {
	@Resource  
    private IUserService userService;  
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
	/**
	 * 管理员登录
	 * @param admin
	 * @param session
	 * @return
	 */
	@ResponseBody
    @RequestMapping("/loginAdmin")  
    public String toIndex(Admin	admin,HttpSession session){  
		admin=userService.adminLogin(admin.getName(),admin.getPwd());
		if(admin!=null){
			session.setAttribute("lastLoginTime", admin.getLastlogintime());
			session.setAttribute("account", "123");
			userService.setTime(df.format(new Date()));
			return "true";  
		}else{
			return "false";  
        }
    }  
	/**
	 * 管理员登录跳转
	 * @param session
	 * @return
	 */
	@RequestMapping("/ForwardAdmin")  
	public String loginForward(HttpSession session){  
		List<Map<String, Object>> countUserList=userService.countUser();
		session.setAttribute("countUser",countUserList);
		List<Map<String, Object>> countTeachList=userService.countTeach();
		session.setAttribute("countTeach",countTeachList);
		List<Map<String, Object>> countApparatusList=userService.countApparatus();
		session.setAttribute("countApparatus",countApparatusList);
		return "sys/main";  
	}  
	/**
	 * 用户列表
	 * @param session
	 * @return
	 */
	@RequestMapping("/userTable")  
	public String userTable(HttpSession session){  
		List<Map<String, Object>> findUserList=userService.findUser();
		session.setAttribute("findUserList",findUserList);
		return "sys/userTable";  
	}  
	/**
	 * 教练列表
	 * @param session
	 * @return
	 */
	@RequestMapping("/teachTable")  
	public String teachTable(HttpSession session){  
		List<Map<String, Object>> findUserList=userService.teachTable();
		session.setAttribute("findTeachList",findUserList);
		return "sys/teachTable";  
	}  
	/**
	 * 器材列表
	 * @param session
	 * @return
	 */
	@RequestMapping("/apparatusTable")  
	public String apparatusTable(HttpSession session){  
		List<Map<String, Object>> findUserList=userService.apparatusTable();
		session.setAttribute("findApparatusList",findUserList);
		return "sys/apparatusTable";  
	}  
	/**
	 * 退出
	 * @return
	 */
    @RequestMapping("/exit")  
    public String exit(HttpSession session){  
    	session.invalidate();
    	return "login";
    } 
    /**
     * 统计性别比例
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSex")  
    public List<User> getSex(){  
    	List<User> list=userService.getSex();
    	return list;  
    }   
    /**
     * 删除用户
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUser")  
    public String delUser(Integer id){  
    	userService.delUser(id);
    	return "true";  
    }   
    /**
     * 删除教练
     * @return
     */
    @ResponseBody
    @RequestMapping("/delTeach")  
    public String delTeach(Integer id){  
    	userService.delTeach(id);
    	return "true";  
    }   
    /**
     * 删除器材
     * @return
     */
    @ResponseBody
    @RequestMapping("/delApp")  
    public String delApp(Integer id){  
    	userService.delApp(id);
    	return "true";  
    }   
    /**
     * 更新用户信息
     * @return
     */
    @RequestMapping("/upUser")  
    public String upUser(Integer id,String account,Integer sex,Integer age,String name,String pwd,String tel,String address,HttpSession session){  
    	
    	List list=new ArrayList();
    	list.add(id);
    	list.add(account);
    	list.add(sex);
    	list.add(age);
    	list.add(name);
    	list.add(pwd);
    	list.add(tel);
    	list.add(address);
    	session.setAttribute("upList", list);
    	return "sys/upUser";  
    }  
    /**
     * 更新教练信息
     * @return
     */
    @RequestMapping("/upTeach")  
    public String upTeach(Integer id,String tname,Integer cid,String ttel,String taddress,HttpSession session){  
    	
    	List list=new ArrayList();
    	list.add(id);
    	list.add(tname);
    	list.add(cid);
    	list.add(ttel);
    	list.add(taddress);
    	session.setAttribute("upTeachList", list);
    	return "sys/upTeach";  
    }  
    /**
     * 添加用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping("/registerUser")  
    public String registerUser(User user){  
    	List<User> id=userService.findAccount(user.getAccount());
    	if(id.size()!=0){
    		return "false";
    	}
    	userService.registerUser(user);
    	return "true";  
    }   
    @RequestMapping("/addUser")  
    public String addUser(){  
    	return "sys/addUser";  
    }   
    /**
     * 添加教练
     * @return
     */
    @RequestMapping("/addTeach")  
    public String addTeach(){  
    	return "sys/addTeach";  
    }   
    /**
     * 添加器材
     * @return
     */
    @RequestMapping("/addApp")  
    public String addApp(){  
    	return "sys/addApp";  
    }   
    /**
     * 更新用户信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/upUserInfo")  
    public String upUserInfo(Integer id,String account,Integer sex,Integer age,String name,String pwd,String tel,String address){  
    	userService.updateUserInfo(id,account,sex,age,name,pwd,tel,address);
    	return "true";  
    }   
    /**
     * 更新教练信息
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/upTeachInfo",method=RequestMethod.POST)  
    public String upTeach(Integer id,String tname,Integer cid,String ttel,String taddress){  
    	userService.upTeach(id,tname,cid,ttel,taddress);
    	return "true";  
    }   
    @ResponseBody
    @RequestMapping("/addTeachInfo")  
    public String addTeachInfo(Teach teach){  
    	userService.addTeachInfo(teach);
    	return "true";  
    }   
    @ResponseBody
    @RequestMapping("/addAppInfo")  
    public String addAppInfo(String aname){  
    	userService.addAppInfo(aname);
    	return "true";  
    }   
}


UserMapper.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.cn.dao.IUserDao">
    <resultMap id="BaseResultMap" type="com.cn.pojo.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="pwd" property="pwd" jdbcType="VARCHAR"/>
        <result column="account" property="account" jdbcType="VARCHAR"/>
        <result column="tel" property="tel" jdbcType="VARCHAR"/>
        <result column="address" property="address" jdbcType="VARCHAR"/>
        <result column="sex" property="sex" jdbcType="INTEGER"/>
        <result column="age" property="age" jdbcType="INTEGER"/>
        <result column="uteach" property="uteach" jdbcType="INTEGER"/>
        <result column="uclass" property="uclass" jdbcType="INTEGER"/>
        <collection property="teachs" javaType="java.util.List"  ofType="com.cn.pojo.Teach">  
            <id column="id" property="id" jdbcType="INTEGER" />  
            <result column="tname" property="tname" jdbcType="VARCHAR" />  
            <result column="ttel" property="ttel" jdbcType="VARCHAR" />  
            <result column="taddress" property="taddress" jdbcType="VARCHAR" />  
            <result column="cid" property="cid" jdbcType="INTEGER"/>
        </collection>
        <collection property="courses" javaType="java.util.List"  ofType="com.cn.pojo.Course">  
            <id column="id" property="id" jdbcType="INTEGER" />  
            <result column="cname" property="cname" jdbcType="VARCHAR" />  
        </collection>
    </resultMap>
        <resultMap id="AdminResultMap" type="com.cn.pojo.Admin">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="pwd" property="pwd" jdbcType="VARCHAR"/>
        <result column="lastlogintime" property="lastlogintime" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="userLogin" resultMap="BaseResultMap" parameterType="com.cn.pojo.User">
        select  id  from user where account = #{0} and pwd = #{1}
    </select>
    <select id="findUserByAccount" resultMap="BaseResultMap" parameterType="com.cn.pojo.User">
        SELECT  u.*,t.*,c.`cname`  FROM user u,teach t,course c WHERE account = #{account} AND u.`uteach`=t.`id` AND u.`uclass`=c.`id`
    </select>
    <select id="findClass"  resultType="HashMap">
        SELECT  * FROM course 
    </select>
    <select id="findSeTeach"  resultType="HashMap">
        SELECT * FROM teach WHERE cid=#{id}
    </select>
    <select id="countUser" resultType="HashMap">
        SELECT COUNT(id) countUser FROM user  
    </select>
    <select id="countTeach" resultType="HashMap">
        SELECT COUNT(id) countTeach FROM teach  
    </select>
    <select id="countApparatus" resultType="HashMap">
        SELECT COUNT(id) countApparatus FROM apparatus   where astate=0
    </select>
    <select id="findAccount" resultType="HashMap">
        SELECT id  FROM user where account=#{account}  
    </select>
    <update id="chooseTeach">
		update user
		<set>
			uteach=#{0},uclass=#{1}
		</set>
		where id=#{2}
	</update>
    <update id="updateUserInfo">
		update user
		<set>
			account=#{account},
			<if test="sex!=null">
      		   sex = ${sex},
      		</if>
			<if test="age!=null">
      		  age = ${age},
      		</if>
			<if test="name!=null">
      		  name = #{name},
      		</if>
			<if test="pwd!=null">
      		  pwd = #{pwd},
      		</if>
			<if test="tel!=null">
      		  tel = #{tel},
      		</if>
			<if test="address!=null">
      		  address = #{address},
      		</if>
		</set>
		where id=#{id}
	</update>
    <update id="upTeach">
		update teach
		<set>
			cid=${cid},
			<if test="tname!=null">
      		  tname = #{tname},
      		</if>
			
			<if test="ttel!=null">
      		  ttel = #{ttel},
      		</if>
			<if test="taddress!=null">
      		  taddress = #{taddress},
      		</if>
		</set>
		where id=#{id}
	</update>
    <select id="adminLogin"  parameterType="com.cn.pojo.Admin" resultMap="AdminResultMap" >
        SELECT lastlogintime FROM admin WHERE name=#{name} and pwd=#{pwd}
    </select>
    <update id="setTime">
		update admin
		<set>
			lastlogintime=#{format}
		</set>
		where name='admin'
	</update>
    <update id="delApp">
		update apparatus
		<set>
			astate=1
		</set>
		where id=#{id}
	</update>
	<select id="getSex" resultType="hashmap">
		SELECT sex ,COUNT(sex) AS num FROM user GROUP BY sex
	</select>
	<select id="findUser" resultType="hashmap">
		SELECT * FROM user 
	</select>
	<select id="teachTable" resultType="hashmap">
		SELECT * FROM teach 
	</select>
	<select id="apparatusTable" resultType="hashmap">
		SELECT * FROM apparatus where astate=0
	</select>
	<insert id="registerUser"  useGeneratedKeys="true">
  		insert into user(id,name, pwd,account,age,sex,tel,address) values(null, #{name}, #{pwd}, #{account}, ${age},${sex},#{tel},#{address})
	</insert>
	<insert id="addTeachInfo"  useGeneratedKeys="true">
  		insert into teach(id,tname,cid,ttel,taddress) values(null, #{tname}, #{cid}, #{ttel}, #{taddress})
	</insert>
	<insert id="addAppInfo"  useGeneratedKeys="true">
  		insert into apparatus(id,aname,astate) values(null, #{aname}, 0)
	</insert>
	<delete id="delUser">
	 DELETE FROM user where id=#{id}
	</delete>
	<delete id="delTeach">
	 DELETE FROM teach where id=#{id}
	</delete>

</mapper>

 


项目DEMO地址

项目演示连接 账号密码详见

 

 

 

 

 

 

 

 

 

Logo

快速构建 Web 应用程序

更多推荐