ssm整合
2019-11-26 11:27:51 66 举报
ssm整合
作者其他创作
大纲/内容
页面
说明要加载的注解的类型
操作数据库
2.调用
UserMapper.javaimport java.util.List;import com.offcn.bean.Student;public interface UserMapper { public List<Student> selectStudent(); public void insertStudent(Student student); public void deleteBySid(Integer sid); public Student selectBySid(Integer sid); public void updateStudent(Student student);}
1.请求
web层(springMvc)Controller.java
log4j配置日志文件
update.jsp(更新页面)<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%><%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %><!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Insert title here</title></head><body><form action=\"${pageContext.request.contextPath }/student/insert\" method=\"post\"> <c:if test=\"${!empty stutent }\"> <input type=\"hidden\" name=\"_method\" value=\"PUT\"> <input type=\"hidden\" name=\"sid\" value=\"${student.sid }\"> </c:if> <table cellpadding=\"10px\"> <caption>个人信息添加</caption> <tr><td>姓名:</td><td><input type=\"text\" name=\"sname\" value=\"${student.sname }\"></td></tr> <tr><td>密码:</td><td><input type=\"text\" name=\"pwd\" value=\"${student.pwd }\"></td> </tr> <tr> <td>性别:</td> <td> <input <c:if test=\"${student.gender == 1 }\">checked</c:if> type=\"radio\" name=\"gender\" value=\"1\">男 <input <c:if test=\"${student.gender == 0 }\">checked</c:if> type=\"radio\" name=\"gender\" value=\"0\">女 </td> </tr> <tr> <td>爱好:</td> <td> <input <c:if test=\"${student.hobby.contains('唱歌') }\">checked</c:if> type=\"checkbox\" name=\"hobby\" value=\"唱歌\">唱歌 <input <c:if test=\"${student.hobby.contains('跳舞') }\">checked</c:if> type=\"checkbox\" name=\"hobby\" value=\"跳舞\">跳舞 <input <c:if test=\"${student.hobby.contains('RAP') }\">checked</c:if> type=\"checkbox\" name=\"hobby\" value=\"RAP\">RAP <input <c:if test=\"${student.hobby.contains('篮球') }\">checked</c:if> type=\"checkbox\" name=\"hobby\" value=\"篮球\">篮球 </td> </tr> <tr> <td>简历:</td> <td> <textarea name=\"mark\" rows=\"\" cols=\"\">${student.mark }</textarea> </td> </tr> <tr> <td colspan=\"2\" align=\"center\"> <input type=\"submit\" value=\"提交\"> </td> </tr> </table></form></body></html>
指定扫描的包
请求dao层
mybatis-config.xml文件(这是普通的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> <settings> <!-- 开启懒加载模式 --> <setting name=\"lazyLoadingEnabled\" value=\"true\"/> <setting name=\"aggressiveLazyLoading\" value=\"false\"/> </settings></configuration>
mybatis-config.xml1.开启懒加载模式 作用:懒加载其实就是延时加载,即当对象需要用到的时候再去加载。
properties文件
beans.xml1.扫描包 作用:加载加了注解的类对象2.加载properties文件3.配置数据源 作用:将properties文件中的信息获取4.配置数据源事务管理器 作用:配置数据源5.开启基于注解的事务支持6.加载mybatis全局配置文件7.扫描Mapper接口 作用:连接dao层的
DaoselectStudent方法
web.xml1.ContextLoaderListener2.spring前端控制器3.CHaracterEncodingFilter4.HiddHttpMethodFilter
UserMapper.xml(数据库操作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\"> <!-- namespace绑定自定义的接口全类名 select中的id绑定接口中得方法 --><mapper namespace=\"com.offcn.mapper.UserMapper\"> <select id=\"selectStudent\" resultType=\"com.offcn.bean.Student\"> select * from student </select> <insert id=\"insertStudent\
页面index.jsp主页面list.jsp显示页面
AppTest.java(测试文件)import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;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;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations= {\"classpath:beans.xml\"})public class AppTest { @Autowired private DataSource dataSource; @Test public void testMethod() throws SQLException { Connection connection = dataSource.getConnection(); System.err.println(connection); connection.close(); }}
dao层(MyBatis)Mapper.java接口Mapper.xml
6.返回ModelAndView
ServiceselectStudent方法
扩展资料-5<property name=\"prefix\" value=\"/WEB-INF/\"></property>该属性设置是将请求的地址加上/WEB-INF/的前缀<property name=\"suffix\" value=\".jsp\
UserController.java(web层)import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import com.offcn.bean.Student;import com.offcn.service.UserService;@Controller@RequestMapping(\"student\")public class UserController { @Autowired private UserService userService; @RequestMapping(value=\"list\
扩展资料-6 <context:component-scan base-package=\"com.offcn\" use-default-filters=\"false\"> <context:include-filter type=\"annotation\" expression=\"org.springframework.stereotype.Controller\"/> <context:include-filter type=\"annotation\" expression=\"org.springframework.web.bind.annotation.ControllerAdvice\
list.jsp页面(展示和操作数据的页面)<%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%><%@ taglib prefix=\"c\" uri=\"http://java.sun.com/jsp/jstl/core\" %><!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Insert title here</title></head><script type=\"text/javascript\" src=\"${pageContext.request.contextPath }/static/js/jquery-1.7.2.js\"></script><script type=\"text/javascript\"> $(function(){ $(\"a[id^=delete]\").click(function(){ var href = $(this).attr(\"href\"); $(\"#form\").attr(\"action\
项目所需的jar包com.springsource.net.sf.cglib-2.2.0.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jarcommons-fileupload-1.3.1.jarcommons-io-2.4.jarcommons-logging-1.1.3.jardruid-1.1.10.jarjackson-annotations-2.1.5.jarjackson-core-2.1.5.jarjackson-databind-2.1.5.jarjstl.jarlog4j-1.2.17.jarmybatis-3.4.1.jarmybatis-spring-1.3.0.jarmysql-connector-java-5.1.42.jarspring-aop-4.0.0.RELEASE.jarspring-beans-4.0.0.RELEASE.jarspring-context-4.0.0.RELEASE.jarspring-core-4.0.0.RELEASE.jarspring-expression-4.0.0.RELEASE.jarspring-jdbc-4.0.0.RELEASE.jarspring-jms-4.0.0.RELEASE.jarspring-orm-4.0.0.RELEASE.jarspring-oxm-4.0.0.RELEASE.jarspring-test-4.0.0.RELEASE.jarspring-tx-4.0.0.RELEASE.jarspring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE.jarstandard.jar
数据库
springmvc.xml1.扫描包2.内部资源视图解析器3.mvc:default-servlet-hanlder4.mvc:annotation-driven
3.调用
实体类Student.java
请求web层
扩展资料-7font color=\"#ff0000\
4.返回list
测试类AppTest.java
beans.xml1.扫描包2.加载properties文件3.配置数据源4.配置数据源事务管理器5.开启基于注解的事务支持6.加载mybatis全局配置文件7.扫描Mapper接口
请求service层
jdbc.properties配置文件jdbc.jdbcUrl=jdbc:mysql://localhost:3306/student?rewriteBatchedStatements=truejdbc.driverClass=com.mysql.jdbc.Driverjdbc.userName=rootjdbc.password=123456
service层(spring)Service.java
log4j.xml(日志配置文件)<?xml version=\"1.0\" encoding=\"UTF-8\" ?><!DOCTYPE log4j:configuration SYSTEM \"log4j.dtd\"> <log4j:configuration xmlns:log4j=\"http://jakarta.apache.org/log4j/\"> <appender name=\"STDOUT\" class=\"org.apache.log4j.ConsoleAppender\"> <param name=\"Encoding\" value=\"UTF-8\" /> <layout class=\"org.apache.log4j.PatternLayout\"> <param name=\"ConversionPattern\" value=\
扩招资料-7
UserService.java(service层)import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.offcn.bean.Student;import com.offcn.mapper.UserMapper;@Servicepublic class UserService { @Autowired private UserMapper userMapper; public List<Student> selectStudent() { return userMapper.selectStudent(); } public void insertStudent(Student student) { userMapper.insertStudent(student); } public void deleteBySid(Integer sid) { userMapper.deleteBySid(sid); } public Student selectBySid(Integer sid) { return userMapper.selectBySid(sid); } public void updateStudent(Student student) { userMapper.updateStudent(student); }}
mybatis-config.xml1.开启懒加载模式
ControllerselectStudent方法
Student.java(实体类)public class Student { private Integer sid; private String sname; private String pwd; private Integer gender; private String hobby; private String mark; getter方法setter方法有参构造无参构造toString方法}
5.返回list
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\"> <!-- 配置spring容器的路径和名称 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置springmvc前段控制器 --> <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>//扩展资料-3 </init-param> <load-on-startup>1</load-on-startup>//扩展资料-4 </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置post请求乱码 --> <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> <!-- 将post请求转化为put请求 --> <filter> <filter-name>HiddenHttpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter> <filter-mapping> <filter-name>HiddenHttpMethodFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
不要将包中带注解的全部加载进容器
收藏
0 条评论
下一页