写点什么

SSM 框架示例(适合新手)

  • 2021 年 11 月 11 日
  • 本文字数:5796 字

    阅读完需:约 19 分钟

}


}


public boolean delete(int id) {


// TODO Auto-generated method stub


int a=usi.delete(id);


if(a==1){


return true;


}else{


return false;


}


}


public boolean update(User u) {


// TODO Auto-generated method stub


int a=usi.update(u);


if(a==1){


return true;


}else{


return false;


}


}


public User findById(int id


【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
复制代码


) {


// TODO Auto-generated method stub


return usi.findById(id);


}


}


6.UserAction? ?包名:lx.action(根据自己的习惯定义就可以)


package lx.action;


import lx.entity.User;


import lx.service.UserSerivce;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Controller;


import org.springframework.web.bind.annotation.RequestMapping;


import org.springframework.web.servlet.ModelAndView;


@Controller


public class UserAction {


@Autowired


private UserSerivce user;


@RequestMapping(value="showuser")


public ModelAndView find(){


//先 new 一个 ModelAndView 对象


ModelAndView mav=new ModelAndView();


//以键值队的形式赋值 然后用对象调用方法


mav.addObject("user",user.findAll());


//要跳转的页面


mav.setViewName("list_user");


//return 出来你 new 的对象


return mav;


}


//增加


@RequestMapping(value="saveuser")


public ModelAndView save(User u){


user.save(u);


return new ModelAndView("redirect:/showuser.do");


}


//删除


@RequestMapping(value="deleteuser")


public ModelAndView delete(int id){


user.delete(id);


return new ModelAndView("redirect:/showuser.do");


}


//预修改


@RequestMapping(value="preupdateuser")


public ModelAndView preupdate(int id){


ModelAndView mav=new ModelAndView();


mav.addObject("user", user.findById(id));


mav.setViewName("update_user");


return mav;


}


//修改


@RequestMapping(value="updateuser")


public ModelAndView update(User u){


user.update(u);


return new ModelAndView("redirect:/showuser.do");


}


}


7.配置 MyBatis 的配置文件??MyBatisConf.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>


<typeAliases>


<typeAlias alias="User" type="lx.entity.User"/>


</typeAliases>


<mappers>


<mapper resource="lx/dao/UserMapper.xml"/>


</mappers>


</configuration>


(2)Spring MVC 的配置文件


SpringConf.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:aop="http://www.springframework.org/schema/aop"


xsi:schemaLocation="


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.2.xsd


http://www.springframework.org/schema/aop


http://www.springframework.org/schema/aop/spring-aop-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.2.xsd">


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >


<property name="location">


<value>classpath:jdbc.properties</value>


</property>


</bean>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">


<property name="driverClassName" value="${jdbc.driver}"></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="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">


<property name="dataSource" ref="dataSource" />


<property name="configLocation" value="classpath:MyBatisConf.xml" />


<!--


它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。


多个 package 之间可以用逗号或者分号等来进行分隔。(value 的值一定要是包的全名)


-->


<property name="typeAliasesPackage" value="lx.entity" />


</bean>


<!-- 注入映射器


为了代替手工使用 SqlSessionDaoSupport 或 SqlSessionTemplate 编写数据访问对象 (DAO)的代码,


MyBatis-Spring 提供了一个动态代理的实现:MapperFactoryBean。这个类 可以让你直接注入数据映射器接口到你的 service 层 bean 中。


当使用映射器时,你仅仅如调 用你的 DAO 一样调用它们就可以了,但是你不需要编写任何 DAO 实现的代码,因为 MyBatis-Spring 将会为你创建代理。


使用注入的映射器代码,在 MyBatis,Spring 或 MyBatis-Spring 上面不会有直接的依赖。


MapperFactoryBean 创建的代理控制开放和关闭 session,翻译任意的异常到 Spring 的 DataAccessException 异常中。


此外,如果需要或参与到一个已经存在活动事务中,代理将 会开启一个新的 Spring 事务。


-->


<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">


<property name="mapperInterface" value="lx.dao.UserDao" />


<property name="sqlSessionFactory" ref="sqlSessionFactory" />


</bean>


<context:component-scan base-package="lx.*" />


</beans>


SpringMVC-servlet.xml


<beans xmlns="http://www.springframework.org/schema/beans"


xmlns:context="http://www.springframework.org/schema/context"


xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"


xsi:schemaLocation="


http://www.springframework.org/schema/util


http://www.springframework.org/schema/util/spring-util-3.2.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd


http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.2.xsd">


mvc:annotation-driven/


<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>


<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>


<context:component-scan base-package="lx.*" />


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">


<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>


</bean>


</beans>


JSP 页面部分:


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>


<a href="showuser.do">查询</a>


</body>


</html>


list_user.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_user.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>


<table border="1">


<tr>


<td>编号</td>


<td>用户名</td>


<td>密码</td>


<td>可执行操作</td>


</tr>


<c:forEach var="user" items="${user}">


<tr>


<td>${user.uid}</td>


<td>${user.uname}</td>


<td>${user.upass}</td>


<td>


<a href="save_user.jsp">增加</a>


<a href="deleteuser.do?id=${user.uid}">删除</a>


<a href="preupdateuser.do?id=${user.uid}">修改</a>


</td>


</tr>


</c:forEach>


</table>


</body>


</html>


save_user.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 'save_user.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>


<form action="saveuser.do">


用户名:<input type="text" name="uname" />


<br>


密码:<input type="text" name="upass" />


<br>


<input type="submit" value="点击提交" />


</form>


</body>


</html>


update_user.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 'update_user.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>


<form action="updateuser.do">


编号:<input type="text" value="${user.uid}" name="uid" />


<br>


用户名:<input type="text" value="${user.uname}" name="uname" />


<br>


密码:<input type="text" value="${user.upass}" name="upass" />


<br>


<input type="submit" value="点击修改" />


</form>


</body>


</html>


<!--


<link rel="stylesheet" type="text/css" href="styles.css">


-->


</head>


<body>


<form action="updateuser.do">


编号:<input type="text" value="${user.uid}" name="uid" />


<br>


用户名:<input type="text" value="${user.uname}" name="uname" />


<br>


密码:<input type="text" value="${user.upass}" name="upass" />


<br>


<input type="submit" value="点击修改" />


</form>


</body>


</html>


补!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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


<listener>


<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>


</listener>


<context-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath:SpringConf.xml</param-value>


</context-param>


<servlet>


<servlet-name>MyDispatcher</servlet-name>


<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>


<init-param>


<param-name>contextConfigLocation</param-name>


<param-value>classpath:SpringMVC-servlet.xml</param-value>


</init-param>


<!-- 标记容器是否在启动的时候就加载这个 servlet。


当值为 0 或者大于 0 时,表示容器在应用启动时就加载这个 servlet;


当是一个负数时或者没有指定时,则指示容器在该 servlet 被选择时才加载。


正数的值越小,启动该 servlet 的优先级越高。


-->


<load-on-startup>1</load-on-startup>


</servlet>


<servlet-mapping>


<servlet-name>MyDispatcher</servlet-name>


<url-pattern>*.do</url-pattern>


</servlet-mapping>


<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>


</web-app>

评论

发布
暂无评论
SSM框架示例(适合新手)