这几天搭建了个自己测试用的框架,springmvc+mybatis,由于使用maven,所以就想尝尝鲜,各种jar包都去搞得最新的,结果悲剧就此上演,首先配置数据库就让差点没吐出血来。记录下来
applicationContext-db.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd" default-autowire="byName"> <!-- 数据源的配置方法一 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias" value="database"/> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="driverUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <!-- 数据源的配置方法二 --> <!-- <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias" value="database"/> <property name="driver" value="${jdbc.driver}" /> <property name="driverUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pc" expression="execution(* com.company.xx.service.impl.*.*(..))" /> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" /> </aop:config> <!-- MyBatis sqlSessionFactory 配置 mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="typeAliasesPackage" value="com.company.xx.bean" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.company.xx.dao" /> <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> --> </bean> <context:component-scan base-package="com.company.xx"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:annotation-config /> <!-- <cache:annotation-driven cache-manager="cacheManager"/> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManagerFactory" ref="cacheManagerFactory" /> </bean> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> --> </beans>
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springmvc</groupId> <artifactId>spring</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 设置spring版本 --> <properties> <org.springframework.version>4.0.2.RELEASE</org.springframework.version> </properties> <dependencies> <!-- 此处开始就是Spring 所有的jar了,spring4.0的jar包拆分了,所以很多 Core utilities used by other modules. Define this if you use Spring Utility APIs (org.springframework.core.*/org.springframework.util.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Expression Language (depends on spring-core) Define this if you use Spring Expression APIs (org.springframework.expression.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Bean Factory and JavaBeans utilities (depends on spring-core) Define this if you use Spring Bean APIs (org.springframework.beans.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Aspect Oriented Programming (AOP) Framework (depends on spring-core, spring-beans) Define this if you use Spring AOP APIs (org.springframework.aop.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Application Context (depends on spring-core, spring-expression, spring-aop, spring-beans) This is the central artifact for Spring’s Dependency Injection Container and is generally always defined --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Various Application Context utilities, including EhCache, JavaMail, Quartz, and Freemarker integration Define this if you need any of these integrations --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Transaction Management Abstraction (depends on spring-core, spring-beans, spring-aop, spring-context) Define this if you use Spring Transactions or DAO Exception Hierarchy (org.springframework.transaction.*/org.springframework.dao.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- JDBC Data Access Library (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you use Spring’s JdbcTemplate API (org.springframework.jdbc.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA, and iBatis. (depends on spring-core, spring-beans, spring-context, spring-tx) Define this if you need ORM (org.springframework.orm.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Object-to-XML Mapping (OXM) abstraction and integration with JAXB, JiBX, Castor, XStream, and XML Beans. (depends on spring-core, spring-beans, spring-context) Define this if you need OXM (org.springframework.oxm.*) <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${org.springframework.version}</version> </dependency> --> <!-- Web application development utilities applicable to both Servlet and Portlet Environments (depends on spring-core, spring-beans, spring-context) Define this if you use Spring MVC, or wish to use Struts, JSF, or another web framework with Spring (org.springframework.web.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Spring MVC for Servlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Servlet Container such as Apache Tomcat (org.springframework.web.servlet.*) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Spring MVC for Portlet Environments (depends on spring-core, spring-beans, spring-context, spring-web) Define this if you use Spring MVC with a Portlet Container (org.springframework.web.portlet.*) --> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc-portlet</artifactId> <version>${org.springframework.version}</version> </dependency> --> <!-- Support for testing Spring applications with tools such as JUnit and TestNG This artifact is generally always defined with a ‘test’ scope for the integration testing framework and unit testing stubs --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>3.2.3.RELEASE</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.5</version> </dependency> <!-- tomcat servlet开发包 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- JSTL标签库 --> <!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> --> <!-- mysql的数据库驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.6</version> </dependency> <!-- 下面两个包proxool是配置数据源的包 --> <dependency> <groupId>com.cloudhopper.proxool</groupId> <artifactId>proxool</artifactId> <version>0.9.1</version> </dependency> <!-- 日志记录依赖包,很多都依赖此包,像log4j,json-lib等等 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.3</version> </dependency> <!-- Spring 文件上传的包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <!-- Spring 文件上传的依赖包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <!-- dom4j 解析 XML文件的包 --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <!-- 下面的三个包是在配置事务的时候用到的 spring的依赖包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.1</version> </dependency> <!-- ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.googlecode.ehcache-spring-annotations</groupId> <artifactId>ehcache-spring-annotations</artifactId> <version>1.2.0</version> </dependency> <!-- JSON lib 开发包 以及它的依赖包 --> <!-- <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- junit 测试包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> <build> <finalName>spring</finalName> <defaultGoal>compile</defaultGoal> </build> </project>
spring-servlet.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName"> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <!-- 扫描@Controller --> <context:component-scan base-package="com.company.xx.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <!-- json处理,防止ie出现下载提示 --> <entry key="html" value="text/html" /> <entry key="json" value="application/json" /> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" /> </list> </property> </bean> <!-- 异常处理 --> <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error/error.jsp" /> </bean> <!-- 文件上传 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000000000"/> </bean> </beans>
jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=root
数据源方案配置一 ,写死连接在xml中是没有问题的。
数据源方案配置二,启动报错如下:
Caused by: java.sql.SQLException: org.logicalcobwebs.proxool.ProxoolException: Couldn't load class ${jdbc.driver}
将default-autowire="byName"去掉,报错如下:
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
奶奶的,不是说sqlSessionFactory是自动注入的么,居然报错。
原来是
mybaits-spring-1.2.2.jar的SqlSessionDaoSupport
/** * Convenient super class for MyBatis SqlSession data access objects. * It gives you access to the template which can then be used to execute SQL methods. * <p> * This class needs a SqlSessionTemplate or a SqlSessionFactory. * If both are set the SqlSessionFactory will be ignored. * <p> * {code Autowired} was removed from setSqlSessionTemplate and setSqlSessionFactory * in version 1.2.0. * * @author Putthibong Boonbong * * @see #setSqlSessionFactory * @see #setSqlSessionTemplate * @see SqlSessionTemplate * @version $Id$ */ public abstract class SqlSessionDaoSupport extends DaoSupport {
* This class needs a SqlSessionTemplate or a SqlSessionFactory
晕死,于是在继承SqlSessionDaoSupport的类中注入sqlSessionFactory
如下:
@Service(value="userService") public class UserServiceImpl extends SqlSessionDaoSupport implements UserService{ /** * sqlSessionFactory setSqlSessionFactory */ @Autowired @Override public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } }
启动就正常了,根据:http://www.oschina.net/code/snippet_1029551_21550
将这个问题记录下,目前还不知道怎么用xml实现
最后的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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 数据源的配置方法一 --> <!-- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias" value="database"/> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="driverUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull" /> <property name="user" value="root" /> <property name="password" value="root" /> </bean> --> <!-- 数据源的配置方法二 --> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <property name="alias" value="database"/> <property name="driver" value="${jdbc.driver}" /> <property name="driverUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="pc" expression="execution(* com.company.xx.service.impl.*.*(..))" /> <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" /> </aop:config> <!-- MyBatis sqlSessionFactory 配置 mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="typeAliasesPackage" value="com.company.xx.bean" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.company.xx.dao" /> <!-- <property name="sqlSessionFactory" value="sqlSessionFactory" /> --> </bean> <context:component-scan base-package="com.company.xx"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <context:annotation-config /> <cache:annotation-driven cache-manager="cacheManager"/> <!-- 这里又被坑了 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManagerFactory" ref="cacheManagerFactory" /> </bean> --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml" /> </bean> </beans>
相关推荐
- mybatis-config.xml:全局配置文件,包括数据源、事务管理器等。 - mapper.xml:SQL 语句和映射规则的配置文件。 - 映射接口:Java 接口,对应 mapper.xml 文件中的操作。 3. **映射机制:** - 动态 SQL:在 ...
这通常包括数据源配置、事务管理器配置以及MyBatis的SqlSessionFactory配置。 2. **配置SpringMVC**: 创建SpringMVC的配置文件,设置DispatcherServlet、视图解析器(如InternalResourceViewResolver)以及...
4. **Oracle数据库**:项目使用Oracle作为后端数据库,这要求开发者熟悉Oracle的SQL语法,包括PL/SQL,以及如何在MyBatis中配置Oracle的连接信息,如数据源、事务管理等。同时,你需要了解Oracle的数据类型、索引、...
6. **配置MyBatis**:创建`mybatis-config.xml`,配置MyBatis的环境信息,如数据源、事务管理器等。编写Mapper接口和对应的Mapper XML文件,定义SQL语句。 7. **编写业务逻辑**:在Java类中实现Controller接口,...
- **数据库连接配置**:配置数据源(DataSource),MyBatis通过数据源获取数据库连接。 - **Mapper配置**:编写Mapper接口和对应的XML配置文件,定义SQL语句和结果映射。 - **Service层**:定义业务逻辑,通过@...
4. **配置MyBatis**:在MyBatis的配置文件mybatis-config.xml中设置数据源、事务管理器,并通过MapperScannerConfigurer扫描Mapper接口。 5. **创建Mapper接口和XML配置**:定义业务操作的Mapper接口,每个方法对应...
在Spring Boot的主配置类中,我们需要配置数据源和MyBatis的SqlSessionFactory。Spring Boot自动配置可以帮助我们轻松完成这些任务。接着,我们需要创建MyBatis的Mapper接口,这些接口将映射到XML配置文件中的SQL...
2. MyBatis的配置文件,包括数据源配置、SqlSessionFactory配置以及Mapper接口和XML映射文件。 3. 业务服务层(Service)和数据访问层(DAO)的实现,利用MyBatis进行数据库操作。 4. 控制器(Controller)类,处理...
本项目"myTest-springmvc_mybaits集成"旨在整合这两个框架,创建一个高效且灵活的后端应用。 首先,我们要理解SpringMVC的工作原理。它采用模型-视图-控制器(MVC)架构模式,其中Controller接收HTTP请求,通过...
在SpringMVC和MyBatis的整合中,MyBatis的SqlSessionFactory和DataSource可以通过Spring的配置来管理,实现数据源的连接池化,提高系统性能。 在本Demo中,你将看到以下几个关键部分: 1. **配置文件**:包括...
在整合这三个框架时,我们需要配置Spring的ApplicationContext,声明SpringMVC的DispatcherServlet,以及MyBatis的相关配置,如数据源、SqlSessionFactoryBean和MapperScannerConfigurer等。此外,还需要在Web.xml中...
SqlConfig.xml:配置Mybatis,包括数据源、事务管理器、Mapper扫描等,定义SQL映射文件的位置。 九、Mybatis Mapper配置 每个Mapper接口对应一个XML映射文件,定义SQL语句和结果映射。 十、测试与调试 完成上述...
此外,还需要配置数据源和事务管理器,以便Spring能够管理和控制事务。 2. **配置SpringMVC**:接着,我们需要配置SpringMVC的相关设置,如配置DispatcherServlet、定义视图解析器、拦截器、处理器映射器和处理器...
3. **src/main/resources**:资源文件夹,存放配置文件如Spring的配置文件(`applicationContext.xml`、`servlet-context.xml`)、MyBatis的配置文件(`mybatis-config.xml`)以及数据源配置(`db.properties`)。...
3. **整合SpringMVC和MyBatis**:首先,需要在SpringMVC的配置文件(如`applicationContext.xml`)中配置MyBatis的相关bean,包括SqlSessionFactory和DataSource。然后,定义Mapper接口,使用@Mapper注解标记,并在...
以及MyBatis的配置文件(mybatis-config.xml),包含数据源、事务管理器以及Mapper扫描器的配置。此外,还需要创建Mapper接口和对应的XML文件,定义SQL语句。 在实现会员管理系统的具体功能时,例如会员注册,用户...
2. **配置MyBatis**:创建MyBatis的配置文件,定义数据源、事务管理器以及SqlSessionFactory。然后,为每个Mapper接口创建对应的XML映射文件,编写SQL语句。在Spring配置文件中,将Mapper接口与XML映射文件关联起来...
这些配置文件定义了组件的依赖关系、bean的实例化、数据源、事务管理等。 2. **模型类(Model)**:如`Article`、`User`等,代表了系统中的实体对象,用于封装业务数据。 3. **Mapper接口(Mapper)**:与数据库...
3. 在MyBatis的配置文件中添加数据源、事务管理器以及Mapper扫描路径。 4. 编写Mapper接口和对应的XML映射文件,定义SQL语句。 5. 在Service层注入Mapper接口,通过接口调用SQL方法。 6. 在Controller层处理请求,...
3. **src/main/resources**:资源文件夹,可能包含Spring的配置文件(如applicationContext.xml、springmvc-servlet.xml)、MyBatis的映射文件(Mapper接口和XML配置)以及数据库连接配置等。 4. **src/main/webapp*...