SSM框架

1.SSM学生信息管理系统示例

 

2. 实验步骤

2.1 建立数据表

 

 

 

2.2 实体类

 

 

 

package cn.edu.data;
public class User{
    private int id;
    private String name;
    private String password;
    
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(){}
    
    public User(int id,String name){
        this.setId(id);
        this.setName(name);
    }
    
    public int getId(){
        return this.id;
    }
    public void setId(int id){
        this.id = id;
    }
    
    public String getName(){
        return this.name;
    }
    
    public void setName(String name){
        this.name = name;
    }
}

    package cn.edu.data;

public class Student {

private  int ID;
    private String studentID;
private String name;
    private String aclass;

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAclass() {
        return aclass;
    }

    public void setAclass(String aclass) {
        this.aclass = aclass;
    }
}

 

2.3 增加映射器

package cn.edu.data;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserMapper {

 @Select("select * from user where name=#{name}")
 public User selectOne(String name);
 
 @Select("select * from user")
 public List<User> selectAll();
 
 @Insert("insert into user (name,password) values (#{name},#{password})")
 public void insert(User user);
 
 @Delete("delete from user where id = #{id}")    
 public void delete(int id);
 
 @Update("update user set name=#{name},password=#{password} where id=#{id} ")
 public void update(User user);
  
 }
package cn.edu.data;
    import java.util.List;
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;
    public interface StudentMapper {
        @Select("select * from student where studentID=#{studentID}")
        public Student selectOne(String studentID);
        @Select("select * from student")
        public List<Student> selectAll();
        @Insert("insert into student (studentID,name,aclass) values (#{studentID},#{name},#{aclass})")
        public void insert(Student student);

        @Delete("delete from student where studentID = #{studentID}")
        public void delete(String studentID );
        @Update("update student set studentID=#{studentID},name=#{name},aclass=#{aclass} where studentID=#{studentID} ")
        public void update(Student student); }

2.4 配置映射器的Bean

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3        xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:mvc="http://www.springframework.org/schema/mvc"
 5        xmlns="http://www.springframework.org/schema/beans"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans 
 7                            http://www.springframework.org/schema/beans/spring-beans.xsd
 8                            http://www.springframework.org/schema/context
 9                            http://www.springframework.org/schema/context/spring-context.xsd
10                            http://www.springframework.org/schema/mvc
11                            http://www.springframework.org/schema/mvc/spring-mvc.xsd " >
12 <!-- 开启注解配置 
13 <context:component-scan base-package="cn.edu.springmvc.test"/>
14 
15 <mvc:annotation-driven enable-matrix-variables="true" />-->
16 
17 <context:component-scan 
18     base-package="cn.edu.springmvc.test,cn.edu.springmvc.login"/>
19 <mvc:annotation-driven enable-matrix-variables="true"/>
20 <!-- 
21 <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" >
22  <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
23  <property name="url" value="jdbc:mysql://localhost:3306/javaee_database" />
24  <property name="username" value="gzy_user"/>
25  <property name="password" value="kk8228221" />
26  <property name="maxTotal" value="20" />
27 </bean>
28  -->
29  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" >
30  <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
31  <property name="url" value="jdbc:mysql://localhost:3306/javaee" />
32  <property name="username" value="root"/>
33  <property name="password" value="123456" />
34  <property name="maxTotal" value="20" />
35 </bean>
36 
37 
38 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
39   <property name="dataSource" ref="dataSource" />
40   <property name="mapperLocations" value="classpath*:cn/edu/data/**/*.xml" />
41 </bean>
42 
43 <bean id="countryMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
44 <property name="mapperInterface" value="cn.edu.data.CountryMapper"/>
45 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
46 </bean>
47 
48 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
49 <property name="mapperInterface" value="cn.edu.data.UserMapper"/>
50 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
51 </bean> 
52 
53 <bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
54 <property name="mapperInterface" value="cn.edu.data.PersonMapper"/>
55 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
56 </bean>
57 
58  
59 <!--使用默认的Servlet来响应静态文件-->
60 <!-- <mvc:default-servlet-handler/>-->
61 <!-- 自定义的SimpleUrlHandlerMapping不起作用的原因是<mvc:default-servlet-handler/>,将其放在最后。-->
62 
63 <bean id="mvcHelloController" name="/hello" class="cn.edu.springmvc.test.MvcHelloController" />   <!--控制器-->
64 
65 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
66     <property name="mappings"> <!--路径依赖-->
67         <props>
68             <prop key="/mvcHello">mvcHelloController</prop>
69         </props>
70     </property>
71 </bean>
72      
73  <!-- 适配器 
74        <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>-->
75 
76 <!--视图解析器-->
77 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
78     <property name="prefix" value="/WEB-INF/view/" /> <!--视图前缀-->
79 <property name="suffix" value=".jsp" /> <!--视图后缀-->
80 </bean>
81 
82 <bean id = "user" class="cn.edu.springmvc.test.User">
83     <constructor-arg index="0" value="2" />
84     <constructor-arg index="1" value="测试用户"/>
85 </bean>
86 
87     <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
88         <property name="mapperInterface" value="cn.edu.data.StudentMapper"/>
89         <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
90 <mvc:default-servlet-handler/>
91 
92 </beans>
93 
94 
95     

2.5 建立视图(jsp)

 

 

 

2.6 建立控制器

 

 

 

  1 package cn.edu.springmvc.login;
  2 import java.util.List;
  3 
  4 import org.springframework.beans.factory.annotation.Autowired;
  5 import org.springframework.stereotype.Controller;
  6 import org.springframework.ui.Model;
  7 import org.springframework.web.bind.annotation.RequestMapping;
  8 import org.springframework.web.servlet.ModelAndView;
  9 import cn.edu.data.*;
 10 
 11 @Controller // 控制器注解
 12 @RequestMapping("/login") // 类中的请求映射注解
 13 public class UserController{ // 控制器类
 14     
 15     //自动装配Mybatis User映射器
 16     @Autowired
 17     UserMapper usermapper;
 18     
 19     @RequestMapping("/check") 
 20     public String  hello(User user, Model model){
 21         // 视图模型对象初始化
 22         ModelAndView modelAndView = new ModelAndView();        
 23         
 24         User user_test= (User)usermapper.selectOne(user.getName());
 25     
 26         if(user_test.getPassword().equals(user.getPassword()))
 27         {
 28             modelAndView.addObject(user);
 29             return "hello";
 30         }
 31         else
 32         {
 33             model.addAttribute("msg", "登录密码或用户名错误!!");
 34             return "ss";
 35         }
 36     }
 37     
 38     //查询全部用户
 39     @RequestMapping("/selectall")
 40     public String  select(Model model){
 41         //ModelAndView modelAndView = new ModelAndView();
 42         List<User> user_list = (List<User>)usermapper.selectAll();
 43         /*for(User user:user_list)
 44             System.out.println(user.getId()+" "+user.getName()+" "+user.getPassword());*/
 45         model.addAttribute("user_list",user_list);
 46         return "showList";
 47     }
 48     
 49     //插入一条记录的录入视图
 50     @RequestMapping("/adduser")
 51     public String addUser() {
 52         return "AddUser";
 53     }
 54     
 55     //插入一条记录
 56     @RequestMapping("/insert")
 57     public String insert(User user) {
 58         usermapper.insert(user);
 59         return "hello";
 60     }
 61     
 62     @RequestMapping("/delete")
 63     public String delete(User user) {
 64         usermapper.delete(user.getId());
 65         return "hello";
 66     }
 67     
 68     @RequestMapping("/update")
 69     public String update(User user) {
 70         usermapper.update(user);
 71         return "hello";
 72     }
 73     
 74     /*
 75     @RequestMapping("/check") // 方法层级的请求注解 {var}是占位符
 76     public ModelAndView hello(@PathVariable int id,@PathVariable String name){
 77           // 路径参数注解,常用在REST风格的编程中
 78          // 视图模型对象初始化
 79         ModelAndView modelAndView = new ModelAndView();
 80         User user = new User(id,name);
 81         modelAndView.setViewName("hello");
 82         modelAndView.addObject(user);
 83         return modelAndView;// 返回视图模型
 84         }
 85     
 86 
 87     
 88     @RequestMapping("/test") // 方法层级的请求注解
 89     public ModelAndView helloUser(User user){// 映射方法  参数自动装箱
 90          // 视图模型对象初始化
 91         ModelAndView modelAndView = new ModelAndView();
 92         modelAndView.setViewName("hello");
 93         modelAndView.addObject(user);
 94         return modelAndView;// 返回视图模型
 95     }
 96     
 97     @GetMapping("/hellomatrix/{username}")
 98     public ModelAndView helloMatrix(
 99             @PathVariable String username,
100             @MatrixVariable(name="userid",pathVar="username") String userid){
101                 // 视图模型对象初始化
102                 System.out.println("UserName:"+username);
103                 System.out.println("UserID:"+userid);
104                 ModelAndView modelAndView = new ModelAndView();
105                 //modelAndView.setViewName("hello");
106                 return modelAndView;// 返回视图模型
107     }
108     
109     // GET /pets/42;q=11;r=22
110 
111     @GetMapping("/pets/{petId}")
112     public void findPet(@PathVariable String petId, @MatrixVariable int q,@MatrixVariable int r) {
113         System.out.println("PetID:"+petId);
114         System.out.println("Q:"+q);
115         System.out.println("R:"+r);
116         // petId == 42
117         // q == 11
118     }*/
119     
120 }
 1 package cn.edu.springmvc.login;
 2 import java.util.List;
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import cn.edu.data.*;
 8 @Controller // 控制器注解
 9 @RequestMapping("/student") // 类中的请求映射注解
10 public class StudentController {
11     // 控制器类
12     // 自动装配Mybatis Student映射器
13     @Autowired
14     StudentMapper studentmapper;
15 
16     //查询全部学生
17     @RequestMapping("/selectall")
18     public String select(Model model) {
19         List<Student> student_list = (List<Student>) studentmapper.selectAll();
20         model.addAttribute("student_list", student_list);
21         return "showStudentList";
22     }
23 
24     //插入一条记录的录入视图
25     @RequestMapping("/addStudent")
26     public String addStudent() {
27 
28         return "addStudent";
29     }
30 
31     //插入一条记录
32     @RequestMapping("/insert")
33     public String insert(Student student) {
34         studentmapper.insert(student);
35         return "redirect:/student/selectall";
36 
37     }
38 
39     @RequestMapping("/delete")
40     public String delete(Student student) {
41         studentmapper.delete(student.getStudentID());
42         return "redirect:/student/selectall";
43         //通过重定向到selectall
44     }
45 
46     @RequestMapping("/update")
47     public String update(Student student) {
48         studentmapper.update(student);
49         return "redirect:/student/selectall"; //通过重定向到selectall
50     }
51 }

3.运行测试

 

 

 

 

 

posted @ 2021-12-03 12:20  坤k  阅读(94)  评论(0)    收藏  举报