第一步,添加所依赖jar
<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>test</groupId> <artifactId>maven-begin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>maven demo begin</name> <description>maven demo begin test all</description> <properties> <!-- Generic properties --> <java.version>1.6</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> <sonar.exclusions>com.mvn.task.one.model, com.mvn.task.tow.model</sonar.exclusions> <!-- Web --> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>2.5</servlet.version> <jackson.version>2.6.1</jackson.version> <!-- Spring --> <spring-framework.version>3.2.3.RELEASE</spring-framework.version> <!-- Hibernate / JPA --> <hibernate.version>4.2.1.Final</hibernate.version> <!-- Logging --> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> <!-- Test --> <junit.version>4.11</junit.version> <!-- MQ --> <mq.version>7.5.0.3</mq.version> </properties> <repositories> <repository> <id>nexus</id> <name>nexus</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Nexus</name> <url>http://localhost:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> <profiles> <profile> <id>testsonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:h2:tcp://localhost:9092/sonar</sonar.jdbc.url> <!-- Optional URL to server. Default value is http://localhost:9000 --> <sonar.host.url> http://localhost:9000 </sonar.host.url> </properties> </profile> <profile> <id>sonar</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8</sonar.jdbc.url> <sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver> <sonar.jdbc.username>root</sonar.jdbc.username> <sonar.jdbc.password>Sonar123</sonar.jdbc.password> <sonar.host.url>http://localhost:9000/</sonar.host.url> <sonar.login>sonaruser</sonar.login> <sonar.password>sonarPassword</sonar.password> </properties> </profile> </profiles> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Other Web dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- Spring and Transactions --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> <!-- <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.5</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- Test Artifacts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- DataBase --> <dependency> <groupId>com.oracle.jdbc</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.185</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>com.codahale.metrics</groupId> <artifactId>metrics-core</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.5</version> </dependency> <!-- MQ --> <!-- <dependency> <groupId>com.ibm</groupId> <artifactId>com.ibm.mq</artifactId> <version>5.3.07</version> </dependency> <dependency> <groupId>com.ibm</groupId> <artifactId>com.ibm.mqjms</artifactId> <version>5.3.07</version> </dependency> <dependency> <groupId>org.jndi</groupId> <artifactId>jndi</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.jta</groupId> <artifactId>jta</artifactId> <version>1.0.1B</version> </dependency> <dependency> <groupId>org.connector</groupId> <artifactId>connector</artifactId> <version>1.0</version> </dependency> --> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mqjms</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.jmqi</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>connector</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>providerutil</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.commonservices</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.defaultconfig</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>dhbcore</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.headers</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>com.ibm.mq</groupId> <artifactId>com.ibm.mq.pcf</artifactId> <version>${mq.version}</version> </dependency> <dependency> <groupId>javax.jms</groupId> <artifactId>jms-api</artifactId> <version>1.1-rev-1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring-framework.version}</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <!-- 是否替换资源中的属性 --> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.txt</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <!-- 此处使用junit的Categories排除集成测试用例 --> <excludedGroups>com.mvn.task.one.IntegrationTest</excludedGroups> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <configuration> <!-- 此处表明测试用例来着test目录所有的java文件 --> <includes> <include>**/*.java</include> </includes> <!-- 此处使用junit的Categories指定只运行集成测试用例 --> <groups>com.mvn.task.one.IntegrationTest</groups> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
第二步,修改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" id="WebApp_ID" version="2.5"> <display-name>maven-begin</display-name> <!-- Spring配置 --> <!-- ====================================== --> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-one.xml,classpath:spring/applicationContext-tow.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring MVC配置 --> <!-- ====================================== --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-one-config.xml,/WEB-INF/springmvc-tow-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
第三步,添加springmvc-one-config.xml,springmvc-tow-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <mvc:annotation-driven /> <context:component-scan base-package="com.mvn.task.one" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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"> <mvc:annotation-driven /> <context:component-scan base-package="com.mvn.task.tow" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
第四步,添加applicationContext-one.xml,applicationContext-tow.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mvn.task.one" /> <!-- <context:component-scan base-package="sonar.source.one" /> --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mvn.task.one.dao" /> <property name="SqlSessionFactoryBeanName" value="sqlSessionFactoryBeanUtils"/> </bean> </beans>
<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="com.mvn.task.tow" /> <!-- <context:component-scan base-package="sonar.source.tow" /> --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mvn.task.tow.dao" /> <property name="SqlSessionFactoryBeanName" value="sqlSessionFactoryBeanAgreement"/> </bean> </beans>
第五步,添加数据源(使用JNDI方式)
package com.mvn.task.one.dao; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; import org.springframework.stereotype.Component; @Configuration @Profile("default") public class DataSourceOne { @Bean(name = "dataSourceUtils") public DataSource dataSourceUtils() { final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup(); dsLookup.setResourceRef(true); return dsLookup.getDataSource("java:jdbc/oracle/DataSource1"); } }
第6步,添加事物
package com.mvn.task.one.dao; import java.io.IOException; import java.sql.SQLException; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Profile; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.EnableTransactionManagement; @Component @EnableTransactionManagement(proxyTargetClass = true) public class DataSqlSessionFactoryOne { @Autowired @Qualifier("dataSourceUtils") private DataSource dataSourceUtils; @Bean(name="sqlSessionFactoryBeanUtils") public SqlSessionFactoryBean sqlSessionFactoryBeanUtils() throws SQLException, IOException { SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSourceUtils); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactory.setMapperLocations(resolver.getResources("com/mvn/task/one/dao/*.xml")); return sqlSessionFactory; } @Bean @Qualifier("utils") public DataSourceTransactionManager transactionManager() throws SQLException{ DataSourceTransactionManager txManager = new DataSourceTransactionManager(); txManager.setDataSource(dataSourceUtils); return txManager; } }
至此,已经完成大半,数据源二和数据源一添加方式一样!上一个项目结构图:
第七步,写一个spring与mybatis的dao接口
package com.mvn.task.one.dao; import java.util.List; import org.springframework.stereotype.Repository; import com.mvn.task.one.model.Subc; @Repository public interface ITestMapper { Test queryTestByParam1Param2(String param1, String param2); }
mybatis配置文件 model实体类先省略了,对照这该配置文件自己可以写出来
<?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.mvn.task.one.dao.ITestMapper"> <select id="queryTestByParam1Param2" parameterType="java.lang.String" resultType="com.mvn.task.one.model.Test"> select ID as id, test1 as test1, test2 as test2 from Test where tes1t = #{0} and test2 = #{1} </select> </mapper>
然后就是service,action,也都是一样的!
相关推荐
Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...
接下来,我们将深入探讨这两个框架以及它们在"SpringMVC+Mybatis demo"中的应用。 **SpringMVC** SpringMVC是Model-View-Controller架构模式的一种实现,用于构建Web应用程序。它的主要组件包括DispatcherServlet...
项目描述 在上家公司自己集成的一套系统,用了两个多月的时间完成的:Springboot+Mybatis-plus+ SpringMvc+Shiro+Redis企业级开发系统 Springboot作为容器,使用mybatis作为持久层框架 使用官方推荐的thymeleaf做为...
基于SpringMVC+Spring+MyBatis个人技术博客系统源码.zip 完整代码,可运行 项目描述 基于SSM实现的一个个人博客系统,适合初学SSM和个人博客制作的同学学习。有了这个源码,直接买了阿里云或腾讯服务器,就可以部署...
springmvc+mybatis+bootstrap框架+oracle数据库 1、兼容BootStrap,兼容Jquery UI。所以可以用bootstrap和jqueryui的功能。当然还有jquery了。 2、图标使用font awesome 3.2,可以使用字体图标 3、表格可以用...
基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM...
基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+...
Spring+SpringMVC+MyBatis整合工程实例 完整版源码,这个SSM框架整合工程是基于IntelliJ IDEA完成的的,工程里面配置文件均有注释,可直接拷贝使用(工程代码可导入IDEA中直接运行),可供学习设计参考。
通过SpringMvc+Spring+Mybatis+Maven整合,学习用maven搭建框架
"Spring+SpringMVC+Mybatis框架项目整合"是一个典型的后端技术栈,旨在提供高效、灵活且可维护的解决方案。下面将详细介绍这三个框架及其整合过程中的关键知识点。 **Spring框架**: Spring是一个开源的Java平台,...
这个"springMVC+mybatis+shiro+redis 项目整合demo"就是一个实例,展示了如何将这些技术集成到一个项目中,以实现高效的数据处理、用户认证授权和缓存管理。 首先,`SpringMVC` 是 Spring 框架的一部分,它是一个...
《Spring+SpringMVC+MyBatis:三位一体的Java企业级开发框架》 在Java企业级应用开发领域,Spring、SpringMVC和MyBatis是三个不可或缺的重要组件,它们共同构建了一个强大的、灵活的和可扩展的应用框架。这篇文章将...
《Spring+SpringMVC+Mybatis 整合实践详解》 在Java Web开发领域,Spring、SpringMVC和Mybatis的整合(简称SSM)是常见的应用架构模式,它为开发者提供了灵活、高效的开发环境。这个名为"spring+springMVC+Mybatis ...
同时,配合博客文章《Spring+SpringMVC+Mybatis框架整合例子》(链接:http://blog.csdn.net/zhshulin/article/details/37956105),读者可以按照教程逐步实践,加深对SSM整合的理解。 总之,SSM框架的整合使得...
标题中的“Spring+SpringMVC+Mybatis框架整合源码”指的是一个基于Java的Web开发项目,它结合了三个主流的开源框架:Spring、SpringMVC和Mybatis,以实现高效且灵活的企业级应用开发。这三种框架在Java世界中扮演着...
本后台管理系统,采用流行的框架springMvc+spring+mybatis+shiro+redis+ehcache开发,实现了权限管理(菜单权限、数据权限),solr全文搜索引擎,activiti工作流程引擎,cas单点登陆等功能,完善的代码生成器 后期还...
这个名为"SpringMVC精品资源--SpringMVC+Mybatis 脚手架.zip"的压缩包很可能包含了一个完整的项目模板或者学习资料,旨在帮助开发者快速搭建基于这两个框架的Web应用。 首先,SpringMVC是一个模型-视图-控制器(MVC...