详解Spring与Mybatis的整合方法(基于Eclipse的搭建)

网友投稿 315 2022-11-17


详解Spring与Mybatis的整合方法(基于Eclipse的搭建)

项目工程总览:

项目路径建的包不是唯一,只要之后配置的路径映射正确即可

Emp.java

5.1.5.RELEASE

3.4.6

1.2.17

junit

junit

4.11

test

org.projectlombok

lombok

1.18.14

provided

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-oxm

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.3.2

mysql

mysql-connector-java

5.1.17

com.mchange

c3p0

0.9.5.5

jstl

jstl

1.2

org.apache.commons

commons-dbcp2

2.7.0

commons-io

commons-io

2.4

commons-logging

commons-logging

1.2

org.junit.jupiter

junit-jupiter

RELEASE

compile

EmpMapper.java 与EmpMapper.xml配置

package com.jektong.dao;

ihttp://mport java.util.List;

import com.jektong.entity.Emp;

/**

* @author jektong

* @Date 2020-10-16 10:13:12

*/

public interface EmpMapper {

List selectAllEmps();

}

EmpMapper.xml配置

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from t_emp

配置数据源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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-4.1.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.1.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc ">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:jdbc="http://springframework.org/schema/jdbc"

xmlns:jee="http://springframework.org/schema/jee"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="

http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.1.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd

http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-4.1.xsd

http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-4.1.xsd

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.1.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.1.xsd

http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-4.1.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.1.xsd

http://springframework.org/schema/jdbc

http://springframework.org/schema/jdbc ">

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 selectAllEmps = mapper.selectAllEmps();

for (Emp emps : selectAllEmps) {

System.out.println(emps.getName());

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:在本地用idea连接虚拟机上的hbase集群的实现代码
下一篇:如何基于FTP4J实现FTPS连接过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~