spring+springMVC+mybatis整合

来源:互联网 发布:wps数据不能求和 编辑:程序博客网 时间:2024/06/18 03:48

spring+springMVC+mybatis整合

一.本人使用的eclipse开发环境

1.1建立的是maven项目,加入必要的依赖关系

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.yao</groupId>  <artifactId>ssm</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>war</packaging>    <properties><junit.version>4.12</junit.version><spring.version>4.3.1.RELEASE</spring.version><mybatis.version>3.2.7</mybatis.version><mybatis-spring.version>1.2.3</mybatis-spring.version><mysql.version>5.1.30</mysql.version><druid.version>1.1.2</druid.version><jackson.version>2.8.9</jackson.version><aspectjweaver.version>1.8.10</aspectjweaver.version><classmate.version>1.3.3</classmate.version><commons-io.version>2.5</commons-io.version><commons-fileupload.version>1.3.2</commons-fileupload.version><gson.version>2.8.1</gson.version><!-- 与hibernate orm无关 --><hibernate-validator.version>5.4.1.Final</hibernate-validator.version><jstl.version>1.2</jstl.version><standard.version>1.1.2</standard.version><commons-net.version>3.3</commons-net.version><dubbo.version>2.5.6</dubbo.version><zookeeper.version>3.4.10</zookeeper.version><zkclient.version>0.10</zkclient.version><!-- 自动实现分页工具包 --><pagehelper.version>4.2.1</pagehelper.version><slf4j-log4j.version>1.7.25</slf4j-log4j.version><jedis.version>2.9.0</jedis.version><solrj.version>4.10.4</solrj.version><servlet.version>3.0.1</servlet.version></properties><!-- 添加依赖 --><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- spring 依赖 begin --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><!-- spring 依赖 end --><!-- mybatis 依赖 begin --><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><!-- mybatis 依赖 end --><!-- mysql begin --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- 数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency></dependencies><build><plugins><!-- jdk编译插件 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin><!-- tomcat运行插件 --><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><path>/</path><port>8080</port></configuration></plugin></plugins></build></project>


二.配置web.xml文件

(前端控制器,后端控制器,字符过滤器)

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">  <display-name>ssm</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>  <!-- 后端控制器 -->  <listener>  <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>    </listener>  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>classpath:spring-*.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.xml</param-value>  </init-param>  </servlet>  <servlet-mapping>  <servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern>  </servlet-mapping>    <!-- 过滤器字符 -->  <filter>  <filter-name>encoding</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>encoding</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>    </web-app>



三.配置公共的配置文件

spring-common.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        ">              <!--读取propietise文件  -->         <context:property-placeholder location="classpath:properties/*.properties"/>                <!-- 扫描service注解驱动 -->        <context:component-scan base-package="com.yao.service"/>        </beans>



四.将mybatis整合到spring中

spring-mybatis.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        ">                        <!-- 会话工厂 -->        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property></bean><!-- 数据源,使用的是阿里公司的数据库连接池的包--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"/>        <property name="username" value="${jdbc.username}"/>        <property name="password" value="${jdbc.password}"/></bean>     <!--mybatis扫描  -->        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage" value="com.yao.mapper"></property>        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>                </bean>                        </beans>


五.整合事务配置文件

spring-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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"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/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        ">                <!-- 事务管理器 -->       <bean id="managedTransaction" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">       <property name="dataSource" ref="dataSource"></property>       </bean>       <!-- 定义通知 -->       <tx:advice id="adiv" transaction-manager="managedTransaction" >       <tx:attributes>       <tx:method name="save*" propagation="REQUIRED"/>       <tx:method name="updata*" propagation="REQUIRED"/>       <tx:method name="del*" propagation="REQUIRED"/>       <tx:method name="*" read-only="true"/>       </tx:attributes>       </tx:advice>              <aop:config>       <aop:pointcut expression="execution(* com.yao.service.*.*(..))" id="point"/>       <aop:advisor advice-ref="adiv" pointcut-ref="point" />       </aop:config>               </beans>

六.springmvc.xml

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        ">        <!-- 开启注解 -->        <mvc:annotation-driven/>        <!-- 扫描注解 -->        <context:component-scan base-package="com.yao.controller"/>                <!-- 视图解析器 -->                <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/"></property>        <property name="suffix" value=".jsp"></property>        </bean>        </beans>

properties文件

jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/testjdbc.username=rootjdbc.password=root

以上是主要的核心配置文件,本人怕比较乱所有分开写了,其实spring-mybatis、spring-common。。这些个配置文件是完全可以一写在一个里面的,不过不在一个里面也是可以相互调用的。

mapper文件省略了比较简单,可以生成。

测试代码

package com.yao.testMapper;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 com.yao.mapper.UserMapper;import com.yao.pojo.User;/** *  * @author T460P *junit集合spring测试 */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath*:spring-*.xml")public class TestMapper {@AutowiredUserMapper userMapper;@Testpublic void insert(){User user=new User();user.setPassword("313");user.setUsername("3333");user.setUserid(9);userMapper.insertSelective(user);}}





没有报错 数据库中已经修改


项目结构




原创粉丝点击