记录一下自己第一次整合smm框架的步骤。
1.数据库使用的是mySql,首先创建数据库ssm1,并创建表student
create database ssm1; use ssm1; CREATE TABLE student( id int(11) NOT NULL AUTO_INCREMENT, student_id int(11) NOT NULL UNIQUE, name varchar(255) NOT NULL, age int(11) NOT NULL, sex varchar(255) NOT NULL, birthday date DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.新建java web项目,命名为ssm1,并且导入相关的jar包。
3.建立pojo类,在这里命名为student,包名为com.ssm1.pojo
1 package com.ssm1.pojo;
2
3 public class Student {
4 private int id;
5 private int student_id;
6 private String name;
7 private int age;
8 private String sex;
9 private String birthday;
10 public int getId() {
11 return id;
12 }
13 public void setId(int id) {
14 this.id = id;
15 }
16 public int getStudent_id() {
17 return student_id;
18 }
19 public void setStudent_id(int student_id) {
20 this.student_id = student_id;
21 }
22 public String getName() {
23 return name;
24 }
25 public void setName(String name) {
26 this.name = name;
27 }
28 public int getAge() {
29 return age;
30 }
31 public void setAge(int age) {
32 this.age = age;
33 }
34 public String getSex() {
35 return sex;
36 }
37 public void setSex(String sex) {
38 this.sex = sex;
39 }
40 public String getBirthday() {
41 return birthday;
42 }
43 public void setBirthday(String birthday) {
44 this.birthday = birthday;
45 }
46
47
48 }
4.建立映射器接口studentMapper,包名为com.ssm1.mapper
1 package com.ssm1.mapper;
2
3 import java.util.List;
4 import com.ssm1.pojo.Student;
5
6 public interface StudentMapper {
7 public int add(Student student);
8
9 public void delete(int id);
10
11 public Student get(int id);
12
13 public int update(Student student);
14
15 public List<Student> list();
16
17 }
5.建立与studentMapper对应的xml文件,同样属于包com.ssm1.mapper
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE mapper
3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
5
6 <mapper namespace="com.ssm1.mapper.StudentMapper">
7
8 <insert id="add" parameterType="Student">
9 INSERT INTO student VALUES(#{student_id},#{name}, #{age}, #{sex}, #{birthday})
10 </insert>
11 <!-- <insert id="add" parameterType="com.ssm1.pojo.Student" useGeneratedKeys="true" keyProperty="id">
12 insert into student
13 <trim prefix="(" suffix=")" suffixOverrides="," >
14 <if test="student_id!=null">
15 student_id,
16 </if>
17 <if test="name!=null and name!=''">
18 name,
19 </if>
20 <if test="age!=null">
21 age,
22 </if>
23 <if test="sex!=null and sex!=''">
24 sex,
25 </if>
26 <if test="birthday!=null and birthday !=''">
27 birthday,
28 </if>
29 </trim>
30 <trim prefix="values (" suffix=")" suffixOverrides="," >
31 <if test="student_id!=null">
32 #{student_id},
33 </if>
34 <if test="name!=null and name!=''">
35 #{name},
36 </if>
37 <if test="age!=null">
38 #{age},
39 </if>
40 <if test="sex!=null and sex!=''">
41 #{sex},
42 </if>
43 <if test="birthday!=null and birthday !=''">
44 #{birthday},
45 </if>
46 </trim>
47 </insert> -->
48
49
50 <delete id="delete" parameterType="Student">
51 delete from student where id= #{id}
52 </delete>
53
54 <select id="get" parameterType="_int" resultType="Student">
55 select * from student where id= #{id}
56 </select>
57
58 <update id="update" parameterType="Student">
59 UPDATE student SET student_id = #{student_id}, name = #{name},
60 age = #{age}, sex = #{sex}, birthday = #{birthday} WHERE id = #{id}
61 </update>
62 <select id="list" resultType="Student">
63 select * from student
64 </select>
65 </mapper>
6.建立studentService接口,包名为com.ssm1.service
1 package com.ssm1.service;
2
3 import java.util.List;
4 import com.ssm1.pojo.Student;
5
6 public interface StudentService {
7 List<Student> list();
8 void add(Student s);
9 void delete(Student s);
10 void update(Student s);
11 Student get(int id);
12 }
7.建立studentServiceImpl类,实现接口,包名为com.ssm1.service
1 package com.ssm1.service;
2
3 import java.util.List;
4
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Service;
7
8 import com.ssm1.mapper.StudentMapper;
9 import com.ssm1.pojo.Student;
10
11 @Service
12 public class StudentServiceImpl implements StudentService {
13 @Autowired
14 StudentMapper studentMapper;
15
16 @Override
17 public List<Student> list() {
18 // TODO Auto-generated method stub
19 return studentMapper.list();
20 }
21
22 @Override
23 public void add(Student s) {
24 // TODO Auto-generated method stub
25 studentMapper.add(s);
26 }
27
28 @Override
29 public void delete(Student s) {
30 // TODO Auto-generated method stub
31 studentMapper.delete(s.getId());
32 }
33
34 @Override
35 public void update(Student s) {
36 // TODO Auto-generated method stub
37 studentMapper.update(s);
38 }
39
40 @Override
41 public Student get(int id) {
42 // TODO Auto-generated method stub
43 return studentMapper.get(id);
44 }
45
46 }
8.建立studentController控制器,包名为com.ssm1.controller
1 package com.ssm1.controller;
2
3 import java.util.List;
4
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.RequestMapping;
8 import org.springframework.web.servlet.ModelAndView;
9
10 import com.github.pagehelper.PageHelper;
11 import com.github.pagehelper.PageInfo;
12 import com.ssm1.pojo.Student;
13 import com.ssm1.service.StudentService;
14 import com.ssm1.util.Page;
15
16 @Controller
17 @RequestMapping("")
18 public class StudentController {
19 @Autowired
20 StudentService studentService;
21
22 @RequestMapping("/index")
23 public ModelAndView index(Page page) {
24 ModelAndView mav = new ModelAndView();
25 List<Student> cs = studentService.list();
26 mav.addObject("cs", cs);
27 mav.setViewName("index");
28 return mav;
29 }
30
31 @RequestMapping(value = "addStudent", produces = "text/html; charset=utf-8")
32 // @RequestMapping("addStudent")
33 public ModelAndView addStudent(Student student) {
34 studentService.add(student);
35 ModelAndView mav = new ModelAndView("redirect:/index");
36 return mav;
37 }
38
39 @RequestMapping("deleteStudent")
40 public ModelAndView deleteStudent(Student student) {
41 studentService.delete(student);
42 ModelAndView mav = new ModelAndView("redirect:/index");
43 return mav;
44 }
45
46 @RequestMapping("editStudent")
47 public ModelAndView editStudent(Student student) {
48 Student s=studentService.get(student.getId());
49 ModelAndView mav=new ModelAndView("editStudent");
50 mav.addObject("s",s);
51 return mav;
52 }
53
54 @RequestMapping("updateStudent")
55 public ModelAndView updateStudent(Student student) {
56 studentService.update(student);
57 ModelAndView mav=new ModelAndView("redirect:/index");
58 return mav;
59
60 }
61 }
9.在WEB-INF目录下建立web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:web="http://java.sun.com/xml/ns/javaee" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 6 7 <!-- spring的配置文件--> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:applicationContext.xml</param-value> 11 </context-param> 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 16 17 <!-- spring mvc核心:分发servlet --> 18 <servlet> 19 <servlet-name>mvc-dispatcher</servlet-name> 20 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 21 <!-- spring mvc的配置文件 --> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:springMVC.xml</param-value> 25 </init-param> 26 <load-on-startup>1</load-on-startup> 27 </servlet> 28 <servlet-mapping> 29 <servlet-name>mvc-dispatcher</servlet-name> 30 <url-pattern>/</url-pattern> 31 </servlet-mapping> 32 <filter> 33 <filter-name>CharacterEncodingFilter</filter-name> 34 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 35 <init-param> 36 <param-name>encoding</param-name> 37 <param-value>utf-8</param-value> 38 </init-param> 39 </filter> 40 <filter-mapping> 41 <filter-name>CharacterEncodingFilter</filter-name> 42 <url-pattern>/*</url-pattern> 43 </filter-mapping> 44 </web-app>
10.在src目录下新建applicationContext.xml文件,这是Spring的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 13 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 14 15 16 17 <!-- 配置@Service包的扫描 --> 18 <context:annotation-config /> 19 <context:component-scan base-package="com.ssm1.service" /> 20 <!-- 配置数据库的连接 --> 21 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 22 <property name="driverClassName"> 23 <value>com.mysql.jdbc.Driver</value> 24 </property> 25 <property name="url"> 26 <value>jdbc:mysql://localhost:3306/ssm1?characterEncoding=UTF-8</value> 27 28 </property> 29 <property name="username"> 30 <value>root</value> 31 </property> 32 <property name="password"> 33 <value>admin</value> 34 </property> 35 </bean> 36 37 <!-- 配置SQLSessionFactory --> 38 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean"> 39 <property name="typeAliasesPackage" value="com.ssm1.pojo" /> 40 <property name="dataSource" ref="dataSource"/> 41 <property name="mapperLocations" value="classpath:com/ssm1/mapper/*.xml"/> 42 </bean> 43 44 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 45 <property name="basePackage" value="com.ssm1.mapper"/> 46 </bean> 47 48 49 50 </beans>
11.在src目录下新增springMVC.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 11 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 12 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 13 14 15 16 <context:annotation-config/> 17 18 <context:component-scan base-package="com.ssm1.controller"> 19 <context:include-filter type="annotation" 20 expression="org.springframework.stereotype.Controller"/> 21 </context:component-scan> 22 23 <mvc:annotation-driven /> 24 25 <mvc:default-servlet-handler /> 26 27 28 <bean 29 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 30 <property name="viewClass" 31 value="org.springframework.web.servlet.view.JstlView" /> 32 <property name="prefix" value="/WEB-INF/jsp/" /> 33 <property name="suffix" value=".jsp" /> 34 </bean> 35 </beans>
12.在WEB-INF下创建jsp目录,并创建文件index.jsp和editStudent.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="java.util.*"%>
3
4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
5 <!DOCTYPE html>
6 <html>
7 <head>
8 <meta charset="UTF-8">
9 <title>Insert title here</title>
10 </head>
11 <body>
12 <table align='center' border='1' cellspacing='0'>
13 <tr>
14 <td>id</td>
15 <td>student_id</td>
16 <td>name</td>
17 <td>age</td>
18 <td>sex</td>
19 <td>birthday</td>
20 <td>编辑</td>
21 <td>删除</td>
22 </tr>
23 <c:forEach items="${cs}" var="c" varStatus="st">
24 <tr>
25 <td>${c.id}</td>
26 <td>${c.student_id}</td>
27 <td>${c.name}</td>
28 <td>${c.age}</td>
29 <td>${c.sex}</td>
30 <td>${c.birthday}</td>
31 <td><a href="editStudent?id=${c.id}">编辑</a></td>
32 <td><a href="deleteStudent?id=${c.id}">删除</a></td>
33
34 </tr>
35 </c:forEach>
36 </table>
37 <div style="text-align:center;margin-top:40px">
38 <form method="post" action="addStudent" >
39 学生学号: <input name="student_id" value="" type="text"> <br><br>
40 学生姓名: <input name="name" value="" type="text"> <br><br>
41 学生年纪: <input name="age" value="" type="text"> <br><br>
42 学生性别: <input name="sex" value="" type="text"> <br><br>
43 学生生日: <input name="birthday" value="" type="text"> <br><br>
44 <input type="submit" value="增加学生">
45 </form>
46 </div>
47 <div style="text-align:center; margin-top:20px">
48 <form action="${pageContext.request.contextPath }/index" method="post">
49 <input value="刷新" type="submit">
50 </form>
51 </div>
52 </body>
53 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8" import="java.util.*"%>
3
4 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="s"%>
5
6 <div style="width:500px;margin:0px auto;text-align:center">
7
8
9 <div style="text-align:center;margin-top:40px">
10
11 <form method="post" action="updateStudent">
12 分类名称: <input name="student_id" value="${s.student_id}" type="text"> <br><br>
13 分类名称: <input name="name" value="${s.name}" type="text"> <br><br>
14 分类名称: <input name="age" value="${s.age}" type="text"> <br><br>
15 分类名称: <input name="sex" value="${s.sex}" type="text"> <br><br>
16 分类名称: <input name="birthday" value="${s.birthday}" type="text"> <br><br>
17 <input type="hidden" value="${s.id}" name="id">
18
19 <input type="submit" value="修改分类">
20 </form>
21
22 </div>
23 </div>
13.最后在tomcat上部署项目,输入路径localhost:端口号/ssm1/index即可访问