SSM整合
Dao spring与Mybatis的整合
思路
配置文件一般有两种一个.properties,另一个是.xml
.properties的特点每一行就是一个键值对
export_dao
(1)properties/db.properties
(2)spring/applicationContext-dao.xml
(3)定义ICompanyDao
(4)定义ICompanyDao.xml
(5)测试
检查pom.xml
查看有没有spring与mybatis的整合包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
properties/db.properties
创建properties文件夹 创建db.properties文件来存放四大信息
DriverManager:驱动
Conection 数据库连接
账户
密码
# key=value
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/saas-export?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
spring/applicationContext-dao.xml
创建spring文件夹 创建applicationContext-dao.xml文件
<?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"
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">
<!--读取db.properties文件-->
<context:property-placeholder location="classpath:properties/db.properties"></context:property-placeholder>
<!--1.配置数据源 拿到四大信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--2.配置Spring整合Mybatis *** 由Spring创建SqlSessionFactory对象 -->
<!--2.1 配置SqlSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"/>
<!-- com.zx.domain.company.Company 简化成company-->
<property name="typeAliasesPackage" value="com.zx.domain"/>
</bean>
<!--2.2 配置Dao接口所在包 动态代理 session.getMapper(Dao.class)-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定Dao接口所在包-->
<property name="basePackage" value="com.zx.dao"/>
</bean>
</beans>
ICompanyDao
package com.zx.dao.company;
import com.zx.domain.company.Company;
import java.util.List;
public interface ICompanyDao {
List<Company> findAll();
}
ICompanyDao.xml
<?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" >
<!--namespace: 需要映射的Dao接口类型-->
<mapper namespace="com.zx.dao.company.ICompanyDao">
<!-- 解决数据库表的字段名与类的变量名不一致-->
<resultMap id="companyMay" type="company">
<id column="id" property="id"/>
<result column="expiration_date" property="expirationDate"/>
<result column="license_id" property="licenseId"/>
<result column="company_size" property="companySize"/>
</resultMap>
<select id="findAll" resultMap="companyMay">
select * from ss_company
</select>
</mapper>
ICompanyDaoTest
spring与service
思路
(1)service依赖dao
(2)编写测试(编写业务逻辑,创建业务对象,调用对象方法,添加spring的单元测试,创建 spring/applicationContext-tx.xml)
(3)如何读取另一个工程的spring的配置
service依赖dao
<dependency>
<artifactId>export_dao</artifactId>
<groupId>com.wzx</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
TestCompanyService
import com.zx.domain.company.Company;
import com.zx.service.company.impl.CompanyServiceImpl;
import com.zx.service.company.inter.ICompanyService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring/applicationContext-*.xml")
public class TestCompanyService {
@Autowired
ICompanyService iCompanyService;
@Test
public void test01(){
/*创建业务对象,调用对象方法*/
List<Company> list= iCompanyService.findAll();
System.out.println("list="+list);
}
}
applicationContext-tx.xml
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描Service实现类-->
<context:component-scan base-package="com.zx.service"/>
<!--Spring声明式事务(底层就是AOP): 三步曲-->
<!--1.配置事务管理器:管理事务:DataSource.Connection.commit() rollback()方法 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源 Dao中配置了-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--2.配置事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<!--配置事务细节特征-->
<tx:attributes>
<!--查询方法,使用默认的隔离级别 及 SUPPORTS(支持事务,如果当前方法有事务就使用,没有也不会创建)传播行为-->
<tx:method name="find*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="query*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="select*" isolation="DEFAULT" propagation="SUPPORTS"/>
<tx:method name="get*" isolation="DEFAULT" propagation="SUPPORTS"/>
<!--增删改方法,使用默认的隔离级别 及 REQUIRED(有使用,没有就开启)传播行为-->
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--3.配置事务切面: 切面=通知+切入点-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt" expression="execution(* com.zx.service.*.impl.*.*(..))"/>
<!--切面=通知+切入点-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
如何读取另一个模块的spring配置
@ContextConfiguration("classpath:spring/applicationContext-*.xml")*
classpath: 加载当前maven工程的resources目录下的配置文件
classpath*: 加载当前maven工程及其依赖工程的resources目录下的配置文
applicationContext-*.xml: 读取所有符合规则的文件
spring整合springmvc
思路
(1)log4j.properties
(2)web.xml
(3)springmvc.xm
(4)CompanyController(创建业务对象,调用业务方法)
log4j.properties
log4j.rootLogger=debug, stdout, logfile
log4j.category.org.springframework=info
#log4j.category.org.apache=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=c:\\log\\myweb.log
log4j.appender.logfile.MaxFileSize=1KB
log4j.appender.logfile.MaxBackupIndex=5
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!--1.spring监听器: 读取applicationContext.xml配置文件-->
<!--修改监听器读取配置路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2.字符编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3.springmvc前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<!--项目启动的时候,创建DispatcherServlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
springmvc.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描Controller所在包-->
<context:component-scan base-package="com.wzx.web"/>
<!--2.视图解析器-->
<!-- success 查找文件 /WEB-INF/pages/success.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--2.1 前缀-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!--2.2 后缀-->
<property name="suffix" value=".jsp"/>
</bean>
<!--3.mvc注解驱动-->
<!--3. 把转换器工厂放入到注解驱动,才会生效的 @RequestMapping @ResponseBody @RequestBody-->
<mvc:annotation-driven />
</beans>
CompanyController
package com.zx.web.controller.company;
import com.zx.dao.company.ICompanyDao;
import com.zx.domain.company.Company;
import com.zx.service.company.inter.ICompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/company")
public class CompanyController {
@Autowired
ICompanyService iCompanyService;
@RequestMapping(path = "/list.do",method = RequestMethod.GET)
public String list(Model model){
List<Company> list= iCompanyService.findAll();
model.addAttribute("list",list);
return "list";
}
}