详解Spring与Mybatis的整合方法(基于Eclipse的搭建)
程序员文章站
2022-06-22 09:26:20
项目工程总览:项目路径建的包不是唯一,只要之后配置的路径映射正确即可emp.java 5.1.5.release...
项目工程总览:
项目路径建的包不是唯一,只要之后配置的路径映射正确
即可
emp.java
<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>
empmapper.java 与empmapper.xml配置
package com.jektong.dao; import java.util.list; import com.jektong.entity.emp; /** * @author jektong * @date 2020-10-16 10:13:12 */ public interface empmapper { list<emp> selectallemps(); }
empmapper.xml配置
<?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.jektong.dao.empmapper"> <resultmap type="emp" id="empmap"> <id property="id" column="e_id" jdbctype="integer" /> <result property="name" column="e_name" jdbctype="varchar" /> <result property="salary" column="e_salary" jdbctype="double" /> <result property="bonus" column="e_bonus" jdbctype="double" /> <result property="hiredate" column="e_hiredte" jdbctype="date" /> <result property="deptno" column="e_deptno" jdbctype="integer" /> </resultmap> <!-- 查询所有emp --> <select id="selectallemps" resulttype="list" resultmap="empmap"> select * from t_emp </select> </mapper>
配置数据源db.properties
文件
# mysql url=jdbc:mysql://localhost:3306/jektong?useunicode=true&characterencoding=utf-8 driver=com.mysql.jdbc.driver username=jektong password=123456
配置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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://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 "> <!-- 加载配置文件并配置数据库连接池 --> <util:properties id="db" location="classpath:db.properties"/> <bean id="datasource" class="org.apache.commons.dbcp2.basicdatasource"> <property name="url" value="#{db.url}"/> <property name="password" value="#{db.password}"/> <property name="username" value="#{db.username}"/> <property name="driverclassname" value="#{db.driver}"/> </bean> <!-- 配置sqlsessionfactorybean --> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> <!-- 引用数据源的组件 --> <property name="datasource" ref="datasource"/> <!-- 映射文件指定 --> <property name="mapperlocations" value="classpath:com/jektong/mapper/*.xml"/> <!-- 类型别名 --> <property name="typealiasespackage" value="com.jektong.entity"/> </bean> <bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> <property name="basepackage" value="com.jektong.dao"/> <property name="sqlsessionfactory" ref="sqlsessionfactory"/> </bean> </beans>
test
package com.jektong.test; import java.util.list; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.jektong.dao.empmapper; import com.jektong.entity.emp; /** * @author jektong * @date 2020-10-15 16:31:58 */ public class testone { @test public void t() throws exception { applicationcontext ac = new classpathxmlapplicationcontext("applicationcontext.xml"); empmapper mapper = (empmapper) ac.getbean("empmapper"); list<emp> selectallemps = mapper.selectallemps(); for (emp emps : selectallemps) { system.out.println(emps.getname()); } } }
到此这篇关于详解spring与mybatis的整合方法(基于eclipse的搭建)的文章就介绍到这了,更多相关spring与mybatis的整合内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!