SSM项目注解形式

 

1.目录

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2.需要导的包

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>springmvc_spring_mybatis_001</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- springmvc(前端控制器)的配置文件 -->
	<!-- 
 	DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,
            而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
	DispatcherServlet主要用作职责调度工作,本身主要用于控制流程,主要职责如下:
	1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
	2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
	3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
	4、通过ViewResolver解析逻辑视图名到具体视图实现;
	5、本地化解析;
	6、渲染具体的视图等;
	7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。
   -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	
	<!-- 过滤通过用于处理项目中的乱码问题,该过滤器位于org.springframework.web.filter包中,指向类CharacterEncodingFilter -->
	<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<description>字符集编码</description>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

4 springmvc.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/tx 
     	   http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
     	   http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
           
        <!-- 包的自动扫描 -->
		<context:component-scan base-package="com.mw.ssm"></context:component-scan>
</beans> 
           
           

5 db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=gbk&useSSL=true&verifyServerCertificate=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456

6 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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
        http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd  
        http://www.springframework.org/schema/aop  
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd">
        
        <!-- 读取属性文件 -->
		<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
			<property name="location" value="classpath:db.properties"></property>
		</bean>
		<!-- 配置dataSource -->
		<bean id = "dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
			<property name="driverClassName" value="${jdbc.driverClass}"></property>
			<property name="url" value="${jdbc.url}"></property>
			<property name="username" value="${jdbc.username}"></property>
			<property name="password" value="${jdbc.password}"></property>

		</bean>
		<!-- <bean id ="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
			<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/school?serverTimezone=GMT"/>
		<property name="username" value="root" />
		<property name="password" value="123456" />
		</bean> -->
		
		
		<!-- 配置sqlSessionFactory: 将spring和mybatis整合-->
		<bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"></property>
			<property name="configLocation" value=""></property>
			<property name="mapperLocations" value="classpath*:com/mw/ssm/xml/*.xml"></property>
			<property name="typeAliasesPackage" value="com.mw.ssm.domain"/>
		</bean>
		
		<!--配置 MapperScannerConfigurer:将Mapper接口生成代理注入到Spring -->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
			<property name="basePackage" value="com.mw.ssm.dao"></property>
		</bean>
		
		<!-- 配置事务管理器 -->
		<bean id = "txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		
		<!--配置事务传播特性 -->
		<tx:advice id = "txAdvice" transaction-manager = "txManage">
			<tx:attributes>
				<tx:method name="get" propagation = "REQUIRED"/>
				<tx:method name="update" propagation = "REQUIRED"/>
				<tx:method name="delete" propagation = "REQUIRED"/>
				<tx:method name="insert" propagation = "REQUIRED"/>
				<tx:method name="*" propagation = "REQUIRED"/>
			</tx:attributes>
		</tx:advice>
		
		<aop:config>
			<aop:pointcut expression="execution(* com.mw.ssm.*.*(..))" id="pointcut"/>
			<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
		</aop:config>
		<!-- 进行包的自动扫描 -->
		<context:component-scan base-package="com.mw.ssm"></context:component-scan>
        
</beans>

7 com.mw.ssm.domain包下建User类

package com.mw.ssm.domain;

public class User {

	private int id;
	private String name;
	private String pwd;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}

8 com.mw.ssm.dao目录下建UserDao接口

package com.mw.ssm.dao;

import java.util.List;
import org.springframework.stereotype.Repository;
import com.mw.ssm.domain.User;

@Repository("userDao")
public interface UserDao {
	
	public List<User> getList();
	public boolean addUser(User user);
	public int deleteUser(int id);
	
}

9 com.mw.ssm.xml包下建User.xml数据访问层文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mw.ssm.dao.UserDao">
	<select id="getList" resultType="User">
		select id,name,pwd from user
	</select>
	<!-- #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,解析成sql时值为order by"111",如果传入的值是id,则解析成的sql为order by "id".
		#方式能够很大程度防止sql注入
	 -->
	<insert id="addUser" parameterType="com.mw.ssm.domain.User" >
		insert into user(id,name,pwd) values(#{id},#{name},#{pwd})
	</insert>
	
	<delete id="deleteUser" parameterType="com.mw.ssm.domain.User" >
    	delete from user where id = #{id}
    </delete>
</mapper>

 

10 com.mw.ssm.service目录下建UserService接口

package com.mw.ssm.service;

import java.util.List;
import com.mw.ssm.domain.User;

public interface UserService {

	public List<User> getList();
	public boolean addUser(User user);
	public String deleteUser(int id);
	
}

11 com.mw.ssm.service.impl目录下建UserService接口的实现类

package com.mw.ssm.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mw.ssm.dao.UserDao;
import com.mw.ssm.domain.User;
import com.mw.ssm.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;
	@Override
	public List<User> getList() {
		return userDao.getList();
	}
	
	@Override
	public boolean addUser(User user) {
		return userDao.addUser(user);
	}

	@Override
	public String deleteUser(int id) {
		if (userDao.deleteUser(id) == 1) {
			return "删除成功";
		}
		return "删除失败";
	}
	
	
}

12 com.mw.ssm.controller包下建控制层UserController类

package com.mw.ssm.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mw.ssm.domain.User;
import com.mw.ssm.service.UserService;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	public void setUserService(UserService userService){
		this.userService = userService;
	}
	
	@RequestMapping("/list")
	public String getList(ModelMap map){
		map.addAttribute("list", userService.getList());
		return "/list.jsp";
	}
	
	@RequestMapping("addUser")
	public String addUser(HttpServletRequest request, User user){
		Boolean boolean1 = userService.addUser(user);
		if (boolean1) {
			return "/list.jsp";
		}else {
			return "/error.jsp";
		}
	}
	 
	@SuppressWarnings("finally")  //用于抑制编译器的警告信息
	@RequestMapping("/deleteUser")
	public String deleteUser(HttpServletRequest request, int id){
		try {
			String str = userService.deleteUser(id);
			request.setAttribute("InfoMessage", str);
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "删除信息失败!具体异常信息:"+e.getMessage());
		}finally {
			return "result";
		}
		
	}
	
}

13 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <h1>---${loginUser}---</h1>
    <a href="add.jsp">新增用户</a>
  </body>
</html>

14 add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加用户</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="<%=request.getContextPath()%>/addUser.do" method="post">
    	用户名:<input type="text"  name="name"/>
    	密&nbsp码:<input type="text"  name="pwd"/>
    	<input type="submit" value="添加用户" />
    </form>
  </body>
</html>

 

15 list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'list.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<title>所有用户所有信息</title>
  </head>
  	
  	
  <body>
  	<h3><center>所有用户所有信息</center></h3>
    <table width="300" border="1" cellpadding="0" align="center">
    	<tr>
    		<td>id</td>
    		<td>姓名</td>
    		<td>密码</td>
    		<td>操作</td>
    	</tr>
    	<tr>
    		<c:forEach items="${list}" var="user">
    			<tr>
    				<td>${user.id }</td>
    				<td>${user.name }</td>
    				<td>${user.pwd }</td>
    				<td><a href="modifyUser?id=${user.id}">更新</a>   <a href="addUser?id=${$user.id}">删除</a></td>
    			</tr>
    		</c:forEach>
    	</tr>
    </table>
    <h5><a href="list">进入用户管理页</a></h5>
    
    <form action="addUser" method="post">
    	<input type="text" name="id">
    	<input type="text" name="name"/>
    	<input type="text" name="pwd"/>
    	<input type="submit" value="添加用户"/>
    </form>
    
    <form action="deleteUser" method="post">
    	<input type="text" name="id"/>
    	<input type="submit" value="删除用户"/>
    </form>
  </body>
</html>

16 result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>结果页</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    ${InfoMessage}
    <a href="<%=basePath%>">返回首页</a>
  </body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值