一.Spring整合Mybatis
1.导包
[1]需要导入的MyBatis的全部包(10个)
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
mybatis-3.2.7.jar
[2]mysql的驱动包(1个)
mysql-connector-java-5.1.30.jar
[3]Spring的包(9个)
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
commons-logging-1.1.3.jar
[4]Spring整合Mybatis需要的包(1个)
mybatis-spring-1.2.3.jar
2.书写配置ApplicationContext.xml文件
[1]连接数据库的内容-------Spring-jdbc
<!-- dataSource连接数据库 --在Spring-jdbc包下去找DataSource 利用set注入相应的属性值>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
[2]创建sqlsessionFactory工厂-----mybatis-spring
<!-- 构建factory工厂
指定dataSource 在mybatis-spring 包下去找SqlSessionFactoryBean查看相应属性,为其赋值,这个factory的功能就相当于以前myBatis的mybatis.xml文件的功能
-->
<bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.bjsxt.pojo"></property>
</bean>
[3]扫描mapper文件-------------mybatis-Spring
<!-- 扫描包 mapper的配置 -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 为其配置basePackage(和之前mybatis.xml文件中的mappers一样,用于配置包或者其他mapper)和 factory-->
<property name="basePackage" value="com.bjsxt.mapper"></property>
<property name="sqlSessionFactory" ref="factory"></property>
</bean>
注意:
配置完成自动的把mapper层的bean类对象创建好 bean 的id名称就是mapper中接口的名称首字母小写
3.mapper层的文件
4.service层的构建
service层的构建也可以利用ApplicationContext.xml文件进行配置
即在service的对应接口中定义set方法,设置一个mapper层的对象进去,在applicationContext.xml中利用set方式注入,而mapper层的实现类对象,spring_IOC容器已经为我们创建好了,即在basePackage配置这个步骤,就是为了自动的把mapper层的bean类对象创建.
<!-- 构建FlowerServiceImpl的对象 -->
<bean id="flowerService" class="com.bjsxt.service.impl.FlowerServiceImpl">
<property name="flowerMapper" ref="flowerMapper"></property>
</bean>
5.书写controller层(书写jsp)
[1]第一种直接new的方式 (在init方法中)
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
studentService = app.getBean("studentService",StudentService.class)
这时出现了一个问题,Spring容器在获取的代码的路径在init方法中的耦合性较高。applicationContext2.xml的文件名变了需要修改代码
这时我们引入了后面两张方式
[2]第二种方式
将Spring容器相关路径信息配置到web.xml中 再通过ServletContext获取初始化参数信息
java代码
String applicationContextName = this.getServletContext().getInitParameter("applicationContextName");
ApplicationContext app = new ClassPathXmlApplicationContext(applicationContextName);
studentService = app.getBean("studentService",StudentService.class);web.xml
<context-param>
<param-name>applicationContextName</param-name>
<param-value>applicationContext2.xml</param-value>
</context-param>
[3]第三种方式
将Spring容器相关路径信息配置到web.xml中,同时配置Spring监听器, 通过spring-web包提供的方法实例化相应的对象,这种方法将所有的配置都放在xml文件中
java代码
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
studentService = app.getBean("studentService",StudentService.class);web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext2.xml</param-value>
</context-param>
<!--需要到spring-web的jar包下面找context包下的ContextLoaderListener 这个类,根据api提示去写相应的配置信息-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
classpath: 是项目class文件根目录的意思
Tips:
ContextLoaderListener作用:在启动Web容器时,自动装配Spring的applicationContext.xml的配置信息。
因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。
如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。
如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数
applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入
网友评论