SSM案例
导入坐标
1 | <properties> |
创建目录结构
创建spring的配置
创建applicationContext.xml
,并导入约束1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--开启注解的扫描,希望处理servlet和dao,controller不需要spring框架处理-->
<context:component-scan base-package="com.xushui">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
测试业务层Spring框架
1 | @Service("accountService") |
Spring测试类1
2
3
4
5
6
7
8
9public class TestSpring {
@Test
public void testAccountService(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService accountService = ac.getBean("accountService",AccountService.class);
accountService.findAll();
}
}
确认Spring框架正常后,进行下一步
SpringMVC框架搭建
引入约束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<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--开启注解扫描,只扫描Controller注解-->
<context:component-scan base-package="com.xushui">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
<!--配置视图解析器对象-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--过滤静态资源-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<!--开启SpringMVC注解的支持-->
<mvc:annotation-driven/>
</beans>
编写Controller,并测试SpringMVC环境1
2
3
4
5
6
7
8
9@Controller
@RequestMapping("/account")
public class AccountController {
@RequestMapping("testSpringmvc")
public String testSpringmvc(){
System.out.println("testSpringmvc执行了...");
return "success";
}
}
Spring整合SpringMVC
目的: 在Controller
中能调用service
对象的方法
- 过程:
- 服务器启动时会创建一个ServletContext域对象,服务器关闭时销毁
- 有一个监听器,监听ServletContext域对象的创建和销毁,它可以在服务器启动时执行一次
- 所以我们可以用这个监听器去加载Spring的配置文件
在web.xml
配置一个监听器,就可以把Spring的配置文件引入1
2
3
4
5
6
7
8
9<!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--设置配置文件的路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
然后就可以在Controller
中用注解给service对象注入
Mybatis框架搭建
编写配置文件SqlMapConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置环境-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="******"/>
</dataSource>
</environment>
</environments>
<!--引入映射配置文件-->
<mappers>
<!--<mapper class="com.xushui.dao.AccountDao"></mapper>-->
<package name="com.xushui.dao"></package>
</mappers>
</configuration>dao用注解形式配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public interface AccountDao {
/**
* 查询所有账户
* @return
*/
@Select("select * from account")
List<Account>findAll();
/**
* 保存账户信息
* @param account
*/
@Insert("insert into account(name, money) values(#{name}, #{money})")
void saveAccount(Account account);
}测试Mybatis环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class TestMybatis {
@Test
public void testFindAll() throws Exception {
// 加载配置文件
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
// 创建SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 创建SqlSession对象
SqlSession sqlSession = factory.openSession();
// 获取到代理对象
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
// 查询所有
List<Account> accounts = accountDao.findAll();
for (Account account : accounts) {
System.out.println(account);
}
// 关闭资源
sqlSession.close();
in.close();
}
}
要注意的是增删改事务要记得提交
Spring整合Mybatis
在Spring的配置文件中配置Mybatis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<!--Spring整合Mybatis框架-->
<!--配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///spring"/>
<property name="user" value="root"/>
<property name="password" value="*******"/>
</bean>
<!--配置SqlSessionFactory工厂-->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置AccountDao接口所在包-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xushui.dao"></property>
</bean>当这边配置好后,就不再需要Mybatis的配置文件
- 配置好后为Dao接口加上注解
@Repository
Spring整合Mybatis第二种
1 | <!--dao层配置文件--> |
配置Spring的生命是事务管理
在Spring配置文件添加1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<!--配置Spring框架声明式事务管理-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--配置AOP增强-->
<aop:config>
<aop:pointcut id="pc1" expression="execution(* com.xushui.service.Impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/>
</aop:config>
在AccountController
中添加saveAccount
方法,用request
和response
完成进行重定向到其他方法1
2
3
4
5
6@RequestMapping("saveAccount")
public void saveAccount(Account account, HttpServletRequest request, HttpServletResponse response) throws Exception {
accountService.saveAccount(account);
response.sendRedirect(request.getContextPath()+"/account/findAll");
return;
}
测试1
2
3
4
5
6<h3>测试保存</h3>
<form action="account/saveAccount" method="post">
名字:<input type="text" name="name">
金额:<input type="text" name="money">
<input type="submit" value="提交保存">
</form>
使用配置文件的方式搭建Mbatis
1 | <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> |
AccountDao.xml1
2
3
4
5
6
7
8
9
10
11
12
13<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xushui.dao.AccountDao">
<select id="findAll" resultType="com.xushui.domain.Account">
select * from account
</select>
<insert id="saveAccount" parameterType="com.xushui.domain.Account">
insert into account(name, money) values(#{name}, #{money})
</insert>
</mapper>
完整applicationContext.xml
1 | <?xml version="1.0" encoding="UTF-8"?> |