您的位置:首页技术文章
文章详情页

详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)

【字号: 日期:2023-08-08 08:21:58浏览:58作者:猪猪

项目结构

详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)

项目路径可以自己定义,只要路径映射正确就可以

pom.xml

<properties> <spring.version>5.1.5.RELEASE</spring.version> <mybatis.version>3.4.6</mybatis.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- 测试包 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.14</version> <scope>provided</scope> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</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> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <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> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.2</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.17</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies>

Emp.java,EmpMapper.java, EmpMapper.xml

https://www.jb51.net/article/197594.htm

db.properties

# mysqljdbc.url=jdbc:mysql://localhost:3306/jektong?useUnicode=true&characterEncoding=utf-8jdbc.driver=com.mysql.jdbc.Driverjdbc.username=jektongjdbc.password=123456

这里的key值不能写url,driver,username,password,不然后面使用${url},${username}会报错

applicationContext.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:jdbc='http://www.springframework.org/schema/jdbc' xmlns:jee='http://www.springframework.org/schema/jee' xmlns:tx='http://www.springframework.org/schema/tx' xmlns:aop='http://www.springframework.org/schema/aop' xmlns:mvc='http://www.springframework.org/schema/mvc' xmlns:util='http://www.springframework.org/schema/util' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc '> <!-- 加载配置文件并配置数据库连接池 --> <context:property-placeholder location='classpath:db.properties'/><!--这里不要使用与name相同的属性名比如写成url--> <bean class='org.apache.commons.dbcp2.BasicDataSource'> <property name='url' value='${jdbc.url}'/> <property name='password' value='${jdbc.password}'/> <property name='username' value='${jdbc.username}'/> <property name='driverClassName' value='${jdbc.driver}'/> </bean> <!-- SqlSessionFactoryBean --> <bean class='org.mybatis.spring.SqlSessionFactoryBean'><!-- 引用数据源的组件 --> <property name='dataSource' ref='dataSource'/> <!-- 映射文件指定 --> <property name='mapperLocations' value='classpath*:mapper/*.xml'/> <!-- 类型别名 --> <property name='typeAliasesPackage' value='com.jektong'/></bean> <!-- sqlSessionFactory已经过时,使用sqlSessionFactoryBeanName,ref改为value --><bean class='org.mybatis.spring.mapper.MapperScannerConfigurer'> <property name='sqlSessionFactoryBeanName' value='sqlSessionFactory'/> <property name='basePackage' value='com.jektong.dao'/></bean></beans>

到此这篇关于详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)的文章就介绍到这了,更多相关Spring与Mybatis整合内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: