Java ssm框架简单实例

项目目录结构

    -LoginDemo
        -src
            -项目主包
                -controller
                -mapper
                -entity
                -service
        -web
            -WEB-INF
                -log4j.properties
                -spring-mybatis.xml
                -springMVC-config.xml
                -web.xml
            -index.jsp
        -pom.xml

需要引入的包
pom.xml
复制代码
  1   2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3 
  4     4.0.0
  5     LoginDemo
  6     com.cyan
  7     ssm
  8     war
  9     1.0-SNAPSHOT
 10     http://maven.apache.org
 11 
 12 
 13     
 14         ssm
 15         
 16             
 17             
 18                 org.mybatis.generator
 19                 mybatis-generator-maven-plugin
 20                 1.3.2
 21                 
 22                     true
 23                     true
 24                 
 25             
 26             
 27                 org.apache.maven.plugins
 28                 maven-compiler-plugin
 29                 
 30                     1.6
 31                     1.6
 32                 
 33             
 34         
 35     
 36 
 37 
 38     
 39         4.1.1.RELEASE
 40     
 41 
 42 
 43 
 44     
 45         
 46         
 47             org.springframework
 48             spring-core
 49             ${spring.version}
 50         
 51 
 52         
 53             org.springframework
 54             spring-web
 55             ${spring.version}
 56         
 57 
 58         
 59             org.springframework
 60             spring-oxm
 61             ${spring.version}
 62         
 63 
 64         
 65             org.springframework
 66             spring-tx
 67             ${spring.version}
 68         
 69 
 70         
 71             org.springframework
 72             spring-jdbc
 73             ${spring.version}
 74         
 75 
 76         
 77             org.springframework
 78             spring-webmvc
 79             ${spring.version}
 80         
 81 
 82         
 83             org.springframework
 84             spring-aop
 85             ${spring.version}
 86         
 87 
 88         
 89             org.springframework
 90             spring-context-support
 91             ${spring.version}
 92         
 93 
 94         
 95             org.springframework
 96             spring-test
 97             ${spring.version}
 98         
 99         
100 
101         
102         
103             org.aspectj
104             aspectjweaver
105             1.8.6
106         
107 
108         
109             org.aspectj
110             aspectjrt
111             1.8.6
112         
113         
114 
115         
116         
117             com.mchange
118             c3p0
119             0.9.5.1
120         
121 
122         
123         
124             javax.servlet
125             servlet-api
126             2.5
127         
128 
129         
130             javax.servlet.jsp
131             jsp-api
132             2.1
133             provided
134         
135         
136 
137         
138         
139             junit
140             junit
141             4.11
142             test
143         
144 
145         
146         
147             org.mybatis
148             mybatis
149             3.3.0
150         
151         
152         
153             org.mybatis
154             mybatis-spring
155             1.2.3
156         
157 
158         
159         
160             mysql
161             mysql-connector-java
162             5.1.6
163         
164 
165         
166         
167             jstl
168             jstl
169             1.2
170         
171 
172     
173 
复制代码

 



配置文件
web.xml

复制代码
 1 
 2  3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6 
 7     
 8     
 9         class>org.springframework.web.context.ContextLoaderListenerclass>
10     
11 
12     
13     
14         springMVC-config
15         class>org.springframework.web.servlet.DispatcherServletclass>
16         
17             contextConfigLocation
18             classpath:/WEB-INF/springMVC-config.xml
19         
20     
21     
22     
23     
24         springMVC-config
25         /
26     
27 
28     
29     
30         contextConfigLocation
31         classpath:WEB-INF/spring-mybatis.xml
32     
33 
34 
复制代码

 

spring-mybatis.xml

复制代码
 1 
 2  3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-3.2.xsd
11         http://www.springframework.org/schema/tx
12         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
13 
14     
15     
16     package="com.cyan" />
17 
18     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19         
20         
21         
22         
23     
24 
25     
26     class="org.mybatis.spring.SqlSessionFactoryBean">
27         
28         
29         
30     
31 
32     
33     class="org.mybatis.spring.mapper.MapperScannerConfigurer">
34         
35     
36 
37 
38     
39     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
40         
41     
42 
43     
44         
45             
46             
47             
48             
49         
50     
51 
52     
53         
54         
55     
56 
57 
58 
复制代码


springMVC-config.xml

复制代码
 1  2        xmlns:context="http://www.springframework.org/schema/context"
 3        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context-3.0.xsd
 9         http://www.springframework.org/schema/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
11 
12     
14     package="com.cyan.Controller">
15         
16         
17     
18 
19     
20     default-servlet-handler/>
21 
22     
23     class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24         
25         
26     
27 
28 
复制代码

 


数据库表结构



详细类设计
-mapper
UserMapper

复制代码
 1 package com.cyan.Mapper;
 2 
 3 import com.cyan.Model.User;
 4 import org.apache.ibatis.annotations.Param;
 5 import org.apache.ibatis.annotations.Select;
 6 
 7 import javax.annotation.Resource;
 8 import java.util.List;
 9 
10 /**
11  * Created by cyan on 16/3/29.
12  */
13 
14 public interface UserMapper {
15     @Select("select * from LoginDemo")
16     public List selectUser();
17     @Select("select * from LoginDemo where username=#{username}")
18     public List selectUserByUsername(@Param("username")String username);
19 
20 }
复制代码

 

-entity
User

复制代码
 1 package com.cyan.Entity;
 2 
 3 /**
 4  * Created by cyan on 16/3/29.
 5  */
 6 public class User {
 7     private int id;
 8     private String username,password,slogan;
 9 
10     public int getId() {
11         return id;
12     }
13 
14     public void setId(int id) {
15         this.id = id;
16     }
17 
18     public String getUsername() {
19         return username;
20     }
21 
22     public void setUsername(String username) {
23         this.username = username;
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password;
32     }
33 
34     public String getSlogan() {
35         return slogan;
36     }
37 
38     public void setSlogan(String slogan) {
39         this.slogan = slogan;
40     }
41 
42 }
复制代码


-service
IUserService

复制代码
 1 package com.cyan.Service;
 2 
 3 import com.cyan.Entity.User;
 4 
 5 /**
 6  * Created by cyan on 16/3/31.
 7  */
 8 public interface IUserService {
 9     public User getUserByName(String name);
10     public boolean verify(String username,String pwd);
11 }
复制代码


UserService

Spring中的几个标签@Component(声明一个类是Spring容器管理的类,可以细分为后面提到的三个标签)、@Controller(控制层)、@Service(服务层)、@Repository(持久层)。标签的作用是让Spring根据名字关联到这个类。

@Autowired标签默认以byType的形式注入,使用这个标签是不需要getter和setter方法的。(这次代码中因为用户名密码校验部分要用到get方法所以写上了)
可以配合@Qualifier标签根据bean的id来装配。

复制代码
 1 package com.cyan.Service;
 2 
 3 import com.cyan.Mapper.UserMapper;
 4 import com.cyan.Entity.User;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 /**
 9  * Created by cyan on 16/3/31.
10  */
11 
12 @Service("userService")
13 public class UserService implements IUserService{
14 
15     @Autowired
16     private UserMapper userMapper;
17 
18     public void setUserMapper(UserMapper userMapper) {
19         this.userMapper = userMapper;
20     }
21 
22     @Override
23     public User getUserByName(String name) {
24         return userMapper.selectUserByUsername(name).get(0);
25     }
26 
27     @Override
28     public boolean verify(String username, String pwd) {
29         if(userMapper.selectUserByUsername(username).get(0).getPassword().equals(pwd))
30             return true;
31         else return false;
32     }
33 
34 }
复制代码



-controller
Login

复制代码
 1 package com.cyan.Controller;
 2 
 3 import com.cyan.Service.IUserService;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Created by cyan on 16/3/29.
14  */
15 
16 @Controller
17 public class Login {
18 
19     @Autowired
20     private IUserService userService;
21 
22     @RequestMapping("/index")
23     public String index(){
24         return "index";
25     }
26 
27     @RequestMapping(value ="/login",method = RequestMethod.POST)
28     public String login(HttpServletRequest req, HttpServletResponse resp){
29         String username=req.getParameter("username");
30         String pwd=req.getParameter("password");
31         if(userService.verify(username,pwd)){
32             req.getSession().setAttribute("user",userService.getUserByName(username));
33             return "success";
34         }
35         else return "index";
36 
37     }
38 }
复制代码


jsp页面
index.jsp

复制代码
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: cyan
 4   Date: 16/3/29
 5   Time: 15:55
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 
10   
11     Login
12   
13   
14     
15 用户名: 16
17 密码: 18
19 20
21 22
复制代码


success.jsp

复制代码
 1 <%@ page import="com.cyan.Entity.User" %><%--
 2   Created by IntelliJ IDEA.
 3   User: cyan
 4   Date: 16/3/31
 5   Time: 23:45
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 
10 
11     LoginSuccess
12 
13 
14 登录成功!
15 <%
16     User user=(User)session.getAttribute("user");
17 %>
18 用户名:<%=user.getUsername()%>
19 个性签名:<%=user.getSlogan()%>
20 21
复制代码



运行结果



你可能感兴趣的:(java)