JavaEESSM框架配置文件
SSM框架理解
最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫不客气地指出。
Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地`new`一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
SSM框架整合
要让几个框架相互配合,配置文件怎么写,项目的目录结构怎么设计对我这样一个新手来说实在很头疼。
目前我也只刚刚写过一个用户登录的demo,在此记录一下。
项目目录结构
-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 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
4 <modelVersion>4.0.0</modelVersion>
5 <name>LoginDemo</name>
6 <groupId>com.cyan</groupId>
7 <artifactId>ssm</artifactId>
8 <packaging>war</packaging>
9 <version>1.0-SNAPSHOT</version>
10 <url>http://maven.apache.org</url>
11
12
13 <build>
14 <finalName>ssm</finalName>
15 <plugins>
16 <!--mybatis 逆向工程插件-->
17 <plugin>
18 <groupId>org.mybatis.generator</groupId>
19 <artifactId>mybatis-generator-maven-plugin</artifactId>
20 <version>1.3.2</version>
21 <configuration>
22 <verbose>true</verbose>
23 <overwrite>true</overwrite>
24 </configuration>
25 </plugin>
26 <plugin>
27 <groupId>org.apache.maven.plugins</groupId>
28 <artifactId>maven-compiler-plugin</artifactId>
29 <configuration>
30 <source>1.6</source>
31 <target>1.6</target>
32 </configuration>
33 </plugin>
34 </plugins>
35 </build>
36
37
38 <properties>
39 <spring.version>4.1.1.RELEASE</spring.version>
40 </properties>
41
42
43
44 <dependencies>
45 <!-- springframe start -->
46 <dependency>
47 <groupId>org.springframework</groupId>
48 <artifactId>spring-core</artifactId>
49 <version>${spring.version}</version>
50 </dependency>
51
52 <dependency>
53 <groupId>org.springframework</groupId>
54 <artifactId>spring-web</artifactId>
55 <version>${spring.version}</version>
56 </dependency>
57
58 <dependency>
59 <groupId>org.springframework</groupId>
60 <artifactId>spring-oxm</artifactId>
61 <version>${spring.version}</version>
62 </dependency>
63
64 <dependency>
65 <groupId>org.springframework</groupId>
66 <artifactId>spring-tx</artifactId>
67 <version>${spring.version}</version>
68 </dependency>
69
70 <dependency>
71 <groupId>org.springframework</groupId>
72 <artifactId>spring-jdbc</artifactId>
73 <version>${spring.version}</version>
74 </dependency>
75
76 <dependency>
77 <groupId>org.springframework</groupId>
78 <artifactId>spring-webmvc</artifactId>
79 <version>${spring.version}</version>
80 </dependency>
81
82 <dependency>
83 <groupId>org.springframework</groupId>
84 <artifactId>spring-aop</artifactId>
85 <version>${spring.version}</version>
86 </dependency>
87
88 <dependency>
89 <groupId>org.springframework</groupId>
90 <artifactId>spring-context-support</artifactId>
91 <version>${spring.version}</version>
92 </dependency>
93
94 <dependency>
95 <groupId>org.springframework</groupId>
96 <artifactId>spring-test</artifactId>
97 <version>${spring.version}</version>
98 </dependency>
99 <!-- springframe end -->
100
101 <!--aspectj start-->
102 <dependency>
103 <groupId>org.aspectj</groupId>
104 <artifactId>aspectjweaver</artifactId>
105 <version>1.8.6</version>
106 </dependency>
107
108 <dependency>
109 <groupId>org.aspectj</groupId>
110 <artifactId>aspectjrt</artifactId>
111 <version>1.8.6</version>
112 </dependency>
113 <!--aspectj end-->
114
115 <!--c3p0-->
116 <dependency>
117 <groupId>com.mchange</groupId>
118 <artifactId>c3p0</artifactId>
119 <version>0.9.5.1</version>
120 </dependency>
121
122 <!--servlet/jsp api start-->
123 <dependency>
124 <groupId>javax.servlet</groupId>
125 <artifactId>servlet-api</artifactId>
126 <version>2.5</version>
127 </dependency>
128
129 <dependency>
130 <groupId>javax.servlet.jsp</groupId>
131 <artifactId>jsp-api</artifactId>
132 <version>2.1</version>
133 <scope>provided</scope>
134 </dependency>
135 <!--servlet/jsp api end-->
136
137 <!--junit4-->
138 <dependency>
139 <groupId>junit</groupId>
140 <artifactId>junit</artifactId>
141 <version>4.11</version>
142 <scope>test</scope>
143 </dependency>
144
145 <!--mybatis-->
146 <dependency>
147 <groupId>org.mybatis</groupId>
148 <artifactId>mybatis</artifactId>
149 <version>3.3.0</version>
150 </dependency>
151 <!--mybatis spring整合-->
152 <dependency>
153 <groupId>org.mybatis</groupId>
154 <artifactId>mybatis-spring</artifactId>
155 <version>1.2.3</version>
156 </dependency>
157
158 <!--mysql driver-->
159 <dependency>
160 <groupId>mysql</groupId>
161 <artifactId>mysql-connector-java</artifactId>
162 <version>5.1.6</version>
163 </dependency>
164
165 <!--jstl-->
166 <dependency>
167 <groupId>jstl</groupId>
168 <artifactId>jstl</artifactId>
169 <version>1.2</version>
170 </dependency>
171
172 </dependencies>
173 </project>
配置文件
web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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 <!-- Spring ApplicationContext 载入 -->
8 <listener>
9 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
10 </listener>
11
12 <!-- SpringMVC核心Servlet -->
13 <servlet>
14 <servlet-name>springMVC-config</servlet-name>
15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
16 <init-param>
17 <param-name>contextConfigLocation</param-name>
18 <param-value>classpath:/WEB-INF/springMVC-config.xml</param-value>
19 </init-param>
20 </servlet>
21
22 <!-- 拦截所有请求 -->
23 <servlet-mapping>
24 <servlet-name>springMVC-config</servlet-name>
25 <url-pattern>/</url-pattern>
26 </servlet-mapping>
27
28 <!-- spring配置文件加载 -->
29 <context-param>
30 <param-name>contextConfigLocation</param-name>
31 <param-value>classpath:WEB-INF/spring-mybatis.xml</param-value>
32 </context-param>
33
34 </web-app>
spring-mybatis.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"
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 <!-- 自动搜索bean -->
15 <context:annotation-config/>
16 <context:component-scan base-package="com.cyan" />
17
18 <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
20 <property name="url" value="jdbc:mysql://localhost:3306/Demo"/>
21 <property name="username" value="root"/>
22 <property name="password" value="2233"/>
23 </bean>
24
25 <!-- mybatis核心bean -->
26 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
27 <property name="dataSource" ref="datasource"/>
28 <!-- 这句配置mapper配置文件的位置 如果采用注解的方式这句可以省去 -->
29 <!--<property name="mapperLocations" value="classpath:/WEB-INF/Mappers/*.xml" />-->
30 </bean>
31
32 <!-- 自动搜索mapper接口 -->
33 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
34 <property name="basePackage" value="com.cyan.Mapper" />
35 </bean>
36
37
38 <!-- 事务处理 -->
39 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
40 <property name="dataSource" ref="datasource"/>
41 </bean>
42
43 <tx:advice id="txAdvice" transaction-manager="txManager">
44 <tx:attributes>
45 <tx:method name="insert*" propagation="REQUIRED"/>
46 <tx:method name="delete*" propagation="REQUIRED"/>
47 <tx:method name="update*" propagation="REQUIRED"/>
48 <tx:method name="select*" propagation="SUPPORTS"/>
49 </tx:attributes>
50 </tx:advice>
51
52 <aop:config>
53 <aop:pointcut id="serviceCut" expression="execution(public * Service.*.*(..))" />
54 <aop:advisor pointcut-ref="serviceCut" advice-ref="txAdvice" />
55 </aop:config>
56
57
58 </beans>
springMVC-config.xml
1 <beans xmlns="http://www.springframework.org/schema/beans"
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 <!--spring可以自动去扫描base-pack下面或者子包下面的java文件,
13 如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean-->
14 <context:component-scan base-package="com.cyan.Controller">
15 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
16 <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
17 </context:component-scan>
18
19 <mvc:annotation-driven />
20 <mvc:default-servlet-handler/>
21
22 <!-- 配置jsp文件的前后缀 “/”代表的是项目设定的Resource目录 -->
23 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
24 <property name="prefix" value="/"/>
25 <property name="suffix" value=".jsp" />
26 </bean>
27
28 </beans>
数据库表结构
详细类设计
-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<User> selectUser();
17 @Select("select * from LoginDemo where username=#{username}")
18 public List<User> 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 <html>
10 <head>
11 <title>Login</title>
12 </head>
13 <body>
14 <form action="/login" method="post">
15 用户名:<input type="text" name="username" id="username"/>
16 <br>
17 密码:<input type="password" name="password" id="password"/>
18 <br>
19 <input type="submit" value="登录"/>
20 </form>
21 </body>
22 </html>
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 <html>
10 <head>
11 <title>LoginSuccess</title>
12 </head>
13 <body>
14 登录成功!
15 <%
16 User user=(User)session.getAttribute("user");
17 %>
18 用户名:<%=user.getUsername()%><br>
19 个性签名:<%=user.getSlogan()%><br>
20 </body>
21 </html>