时间:2021-05-19
项目工程总览:
项目路径建的包不是唯一,只要之后配置的路径映射正确即可
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文件
# mysqlurl=jdbc:mysql://localhost:3306/jektong?useUnicode=true&characterEncoding=utf-8driver=com.mysql.jdbc.Driverusername=jektongpassword=123456配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://.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的整合内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文主要介绍Spring与Mybatis三种常用整合方法,需要的整合架包是mybatis-spring.jar,可通过链接http://code.google.
为了梳理前面学习的《Spring整合MyBatis(Maven+MySQL)一》与《Spring整合MyBatis(Maven+MySQL)二》中的内容,准备做
spring和mybatis整合整合思路需要spring通过单例方式管理SqlSessionFactory。spring和mybatis整合生成代理对象,使用S
最近想学习AngularJS的使用,网上搜了一圈后,折腾了半天解决bug后,成功使用AngularJS整合Springmvc、Spring、Mybatis搭建了
昨天介绍了mybatis与spring的整合,今天我们完成剩下的springmvc的整合工作。要整合springmvc首先得在web.xml中配置springm