标签:
本文记录了Spring+SpringMVC+MyBatis+Maven框架整合的记录,主要记录以下几点
一、Maven需要引入的jar包
二、Spring与SpringMVC的配置分离
三、Spring与MyBatis的整合
一、Maven需要引入的jar包
本文默认读者已经掌握Maven的使用,Maven配置片段如下
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${springframework.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>${mybatis-spring.version}</version>
- </dependency>
- <dependency>
- <groupId>com.oracle</groupId>
- <artifactId>ojdbc14</artifactId>
- <version>${oracle14.version}</version>
- </dependency>
- <dependency>
- <groupId>c3p0</groupId>
- <artifactId>c3p0</artifactId>
- <version>${c3p0.version}</version>
- </dependency>
二、Spring与SpringMVC的配置分离
1、有必要说明一下,web.xml中配置的执行顺序:
listener>filter>servlet,而同一种配置片段则按照从上到下的顺序执行。
下载地址 主流的Java后台 SSM 框架 springmvc spring mybatis 项目
2、web.xml的配置片段,下面的配置信息将Spring与SpringMVC的配置分别放到了applicationContext*.xml和springmvc-servlet.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>
-
- <servlet>
- <servlet-name>springmvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:springmvc-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>springmvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
-
- <filter>
- <filter-name>encodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
3、springmvc的配置片段如下,springmvc-servlet.xml
- <context:component-scan base-package="org.jisonami.controller">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- p:prefix="/WEB-INF/content/" p:suffix=".jsp">
- </bean>
4、目前本例中只是用了一个spring配置文件,即applicationContext.xml,如下:
<!-- 自动扫描spring注解,排除springmvc已扫描的Controller注解 -->
- <context:component-scan base-package="org.jisonami">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- </context:component-scan>
5、关于spring配置文件中的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:p="http://www.springframework.org/schema/p"
- 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">
6、此时已经可以在代码中使用注解来配置spring的bean了,即如下形式的代码完成依赖注入:
- @Autowired
- private UserService userService;
三、Spring与MyBatis的整合
1、MyBatis的使用主要是使用Mapper接口+Mapper.xml中写sql的方式来实现更灵活的dao层,这一部分在与spring整合之后是不变的
而mybatis的全局配置文件则是SqlMapConfig.xml,也可以是其它的名字。
2、整合之前,数据库的连接信息是在SqlMapConfig.xml中配置的,并且Mapper的扫描也是在SqlMapConfig.xml中配置的
最麻烦的是我们完成crud操作的代码有比较多的冗余,即如下所示的形式:
- InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
- SqlSession session = sessionFactory.openSession();
- UserMapper userMapper = session.getMapper(UserMapper.class);
- userMapper.save(user);
- session.commit();
- session.close();
3、与spring整合的目的则是将SqlSessionFactory、SqlSession、UserMapper的创建和SqlSession的事物提交与关闭交给spring容器进行管理,
实现只需要调用一行代码的效果,即
4、整合之后,数据库的连接信息与Mapper的扫描的配置片段直接移到applicationContext.xml中去了,完成SqlSessionFactory、SqlSession、UserMapper注入
applicationContext.xml中mybatis的配置片段:
- <context:property-placeholder location="classpath:DBConfig.properties"/>
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
- <property name="driverClass" value="${jdbc.driver}"></property>
- <property name="jdbcUrl" value="${jdbc.url}"></property>
- <property name="user" value="${jdbc.user}"></property>
- <property name="password" value="${jdbc.pass}"></property>
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="configLocation" value="classpath:SqlMapConfig.xml" />
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="org.jisonami.mybatis.mapper"></property>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
- </bean>
5、原来的SqlMapConfig.xml文件中只剩下寥寥几行配置信息,
- <configuration>
-
- <typeAliases>
- <package name="org.jisonami.entity"/>
- </typeAliases>
- </configuration>
6、以UserMapper.xml配置片段为例,描述整合后的简洁编程方式
- <mapper namespace="org.jisonami.mybatis.mapper.UserMapper">
-
-
- <insert id="save" parameterType="User">
- <selectKey keyColumn="id" keyProperty="id" resultType="String" order="BEFORE">
- select sys_guid() from dual
- </selectKey>
- insert into t_user(id, name, password) values(#{id}, #{name}, #{password})
- </insert>
- </mapper>
UserMapper接口中的方法声明如下:
- public interface UserMapper {
- public void save(User user);
- }
调用部分代码,直接注入UserMapper
- @Autowired
- private UserMapper userMapper;
- public void save(User user) {
- userMapper.save(user);
- }
Spring+SpringMVC+MyBatis+Maven框架整合
标签:
原文地址:http://www.cnblogs.com/yunanz5525/p/5461123.html