Spring MVC之——SSM整合
搭建整合环境
详细信息可以进入以下网址查看
http://www.mybatis.org/spring/
整合说明:SSM整合可以使用多种方式,咱们选择XML + 注解的方式
整合的思路
- 先搭建整合的环境
- 先把Spring的配置搭建完成
- 再使用Spring整合SpringMVC框架
- 最后使用Spring整合MyBatis框架
ps:其实都无所谓啦。。。下面为了看起来不臃肿,代码不重复放出来
pom.xml配置文件
1 | <dependencies> |
log4j配置文件
1 | # Set root category priority to INFO and its only appender to CONSOLE. |
web.xml配置文件
1 | <!-- SpringMVC的入口是servlet,这里配置SpringMVC --> |
1、搭建Spring + Mybatis
- Userdao类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36package pres.liuhaoan.ssm.dao;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import pres.liuhaoan.ssm.domain.User;
import java.util.List;
/**
* User表操作的Dao类
*/
@Repository
public interface UserDao {
/**
* 查询所有
* @return user列表
*/
@Select("select * from user")
@Results(id = "userMap", value = {
@Result(column = "username", property = "username"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address")})
List<User> findAll();
/**
* 新增账户
* @param user 用户实体类信息
* @return 返回自增索引
*/
@Insert("insert into user(username, birthday, sex, address) values(#{username}, #{birthday}, #{sex}, #{address})")
@SelectKey(statement = {"select last_insert_id()"}, keyColumn = "id", keyProperty = "id", before = false, resultType = Integer.class)
int insertUser(User user);
} - UserService类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32package pres.liuhaoan.ssm.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import pres.liuhaoan.ssm.dao.UserDao;
import pres.liuhaoan.ssm.domain.User;
import pres.liuhaoan.ssm.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private final UserDao userDao;
public UserServiceImpl( UserDao userDao) {
this.userDao = userDao;
}
public List<User> findAll() {
return userDao.findAll();
}
public int insertUser(User user) {
return userDao.insertUser(user);
}
} - jdbcConfig.properties配置文件
1
2
3
4username=root
password=
url=jdbc:mysql://localhost/test?characterEncoding=UTF-8
driver=com.mysql.jdbc.Driver - applicationContext.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置Spring容器的扫描注解 -->
<context:component-scan base-package="pres.liuhaoan">
<!--配置不扫描SpringMVC的控制器注解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 以下是整合Mybatis的配置 -->
<!-- 配置c3p0数据源 -->
<context:property-placeholder location="classpath:jdbcConfig.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${url}"></property>
</bean>
<!-- 配置SqlSession的工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置扫描dao的包 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="pres.liuhaoan.ssm.dao"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="*"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切入点表达式 -->
<aop:pointcut id="pt1" expression="execution(* pres.liuhaoan.ssm.service.impl.*.*(..))"/>
<!-- 建立切入点和通知的关系 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
2、搭建SpringMVC
- StringToDateConverter类型转换器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28package pres.liuhaoan.ssm.utils;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 自定义类型转换
*/
public class StringToDateConverter implements Converter<String, Date> {
public Date convert(String s) {
System.out.println(s);
DateFormat format = null;
try {
if(s == null) {
throw new NullPointerException("请输入要转换的日期");
}
format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return format.parse(s);
} catch (Exception e) {
throw new RuntimeException("输入日期有误");
}
}
} - springmvc.xml配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置spring创建容器时要扫描的包 -->
<context:component-scan base-package="pres.liuhaoan.ssm.controller">
<!--<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>-->
</context:component-scan>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".html"></property>
</bean>
<!-- 配置spring开启注解mvc的支持-->
<mvc:annotation-driven conversion-service="converterService"></mvc:annotation-driven>
<!-- 设置静态资源不过滤,方式一 -->
<mvc:default-servlet-handler/>
<!-- 设置静态资源不过滤,方式二-->
<!--<mvc:resources location="/css/" mapping="/css/**"/> <!– 样式 –>
<mvc:resources location="/images/" mapping="/images/**"/> <!– 图片 –>
<mvc:resources location="/js/" mapping="/js/**"/> <!– javascript –>-->
<!-- 配置自定义类型转换器 -->
<bean id="converterService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<array>
<bean class="pres.liuhaoan.ssm.utils.StringToDataConverter"></bean>
</array>
</property>
</bean>
</beans> - 前端index.html文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello
<a href="user/findAll">查询所有</a>
<form action="user/addUser" method="post">
姓名:<input name="username" type="text">
生日:<input name="birthday" type="text">
性别:<input name="sex" type="text">
地址:<input name="address" type="text">
<input value="提交" type="submit">
</form>
</body>
</html>