轉自:http://www.cnblogs.com/verlen11/p/5349747.htmlhtml
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命令。
java
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.xmlmysql
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.xmlweb
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.xmlspring
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>複製代碼
數據庫表結構
sql
詳細類設計
-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
express
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
IUserServiceapache
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>複製代碼
運行結果
相關學習資料移步: