spring与mybatis整合

本文详细介绍了如何将Spring与Mybatis进行整合。首先强调了mybatis-spring.jar的重要性,接着展示了工程目录结构。然后逐步讲解了创建数据库表、编写实体类、定义接口与mapper.xml文件、配置主配置文件和Spring配置文件的过程。最后提到了测试类的编写和log4j.properties文件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步:导包
由于包过多,这里就不一一介绍
注意:这里不能少mybatis-spring.jar这个包,这个包是用来联系Spring和Mybatis的
在这里插入图片描述
整个工程目录如下:
在这里插入图片描述
第二步:创建两张表(本人两张表分别为user和order),如下图所示:
user表:
在这里插入图片描述
order表:
在这里插入图片描述
第三步:编写相对应的实体类:
建议实体类名与表名同名(首字母大写)
User类:

package com.zhiyuan.Bean;

public class User {
	private int id;
	private String username;
	private int mobile;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public int getMobile() {
		return mobile;
	}
	public void setMobile(int mobile) {
		this.mobile = mobile;
	}
}

Order类:

package com.zhiyuan.Bean;

public class Order {
	private int oId;
	private int uId;
	private String orderNo;
	private double money;
	private User user;
	public int getoId() {
		return oId;
	}
	public void setoId(int oId) {
		this.oId = oId;
	}
	public int getuId() {
		return uId;
	}
	public void setuId(int uId) {
		this.uId = uId;
	}
	public String getOrderNo() {
		return orderNo;
	}
	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

第四步:编写接口与mapper.xml文件
IUser接口:

package com.zhiyuan.Dao;

import java.util.List;

import com.zhiyuan.Bean.Order;

public interface IUser {
	public List<Order> GetOrderID(int id);
	//List<Order>:用来接收order表中的多条数据
}

order.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "mybatis-3-mapper" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.Dao.IUser">
<!--  namespace:命名空间,com.zhiyuan.Dao.IUser:接口路径 -->
	<resultMap type="order" id="myMap">
	<!-- type:如果在主配置文件中配置了别名,这里可以写别名,如没有配置这里写Order类全路径 -->
		<result property="oId" column="order_id"/>
		<!-- property:与实体类中的属性名保持一致,column与数据库里面的字段名保持一致 -->
		<result property="uId" column="user_id"/>
		<result property="orderNo" column="order_no"/>
		<result property="money" column="money"/>
		<association property="user" javaType="user" column="user_id">
		<!-- 一对多用collection,多对一用association -->
		<!-- ofType:和上面的type一样,column:user表中与order表有关系的字段 -->
			<result property="id" column="u_id"/>
			<result property="username" column="username"/>
			<result property="mobile" column="mobile"/>
		</association>
	</resultMap>

	<select id="GetOrderID" parameterType="int" resultMap="myMap">
	<!-- id:唯一标识符,parameterType:传入参数类型,resultMap:本人理解为自定义返回参数类型,详细可以参考:http://www.cnblogs.com/fsjohnhuang/p/4076592.html,resultMap中aa与resultMap泛型id值保持一致  -->
		select * from user,`order` where user.u_id=`order`.user_id and `user`.u_id=#{id}
		<!-- 查询语句,#{id}:表示占位符 -->
	</select>
</mapper>

第五步:编写config主配置文件
之前我们这里配置了三大块,分别是:别名,基础环境配置,映射文件,现在我们将我们的基础环境配置交给我们的spring容器来进行管理,所以我们这里的config主配置文件会有所不一样,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "mybatis-3-config" "mybatis-3-config.dtd" >
<configuration>
	<!-- 设置别名 -->
	<typeAliases>
		<package name="com.zhiyuan.Bean"/>
		<!-- 批量定义别名:
		name:指定包名,mybatis自动扫描包中的类,自动定义别名,别名就是类名(首字母大小写都可以) 
		-->
	</typeAliases>
	
	<!-- 映射文件配置 -->
	<mappers>
		<mapper resource="com/zhiyuan/Dao/order.xml"/>
	</mappers>
</configuration>

第六步:编写spring配置文件
springDefinition.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:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
		default-autowire="byName">
		<!-- default-autowire="byName":自动装配 -->
		
	<!-- 数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/springmybaitszhenghe"></property>
		<property name="username" value="root"></property>
		<property name="password" value="1234"></property>
	</bean>
	
	<!-- 回话工厂 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 数据源 -->
		<property name="configLocation" value="config.xml"></property>
		<!-- 加载mybatis的配置文件 -->
	</bean>
	
	<!-- mapper配置
		MapperFactoryBean:根据mapper接口生成代理对象
		使用此方法的问题就是需要对每一个mapper进行配置,麻烦,如果有多个mapper就可以使用下面的mapper批量扫描
	 -->
	<bean id="iuser" class="org.mybatis.spring.mapper.MapperFactoryBean">
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
		<property name="mapperInterface" value="com.zhiyuan.Dao.IUser"></property>
		<!-- mapperInterface指定mapper接口 -->
	</bean>
	
	<!-- 
	mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 
	遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在同一目录中
	自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
	使用此方法可以不用在config.xml配置文件中扫描映射文件
	-->
	<!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="包名"></property>
		指定扫描的包名
		如果扫描多个包,每个包中间使用半角逗号隔开
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean> -->
	
</beans>

第七步:编写测试类
Text类:

package com.zhiyuan.Text;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiyuan.Bean.Order;
import com.zhiyuan.Bean.User;
import com.zhiyuan.Dao.IUser;

public class Text {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("springDefinition.xml");
		//创建一个容器对象
		IUser iuser = (IUser) ac.getBean("iuser");
		//iuser是springDefinition.xml里面的id名
		//从容器对象中拿到Bean类的对象,并用接口对象接收
		List<Order> list = iuser.GetOrderID(1);
		//使用接口对象调用接口中的方法并传入参数,再用接口的返回参数类型来进行接收
		User user = null;
		//引用User类,作用:获取下面order.getUser()方法
		for (Order order : list) {
		//使用for循环将list集合中的值一一打印出来
			System.out.println("订单号:"+order.getOrderNo()+"  价格:"+order.getMoney());
			user = order.getUser();
			//用Order类的对象名调用Order类中的User,再将获取到的方法交给上面的User类
		}
		System.out.println("用户名"+user.getUsername());
		//再用user属性名获取User类里面的值
	}

}

log4j.properties文件
该文件里面的内容无需理解

log4j.rootLogger=WARN, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值