Spring与Mybatis集成

以用户对应订单信息为例——一对多

第一步:创建两张数据库表分别取名为User和Order

第二步:并给User表和Order表中添加数据

第三步:创建一个Java工程,并导入相关包

第四步:创建两张数据表对应的实体类

package com.zhiyuan.pojo;

public class User {
	private int id;
	private String username;
	private String 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 String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
}

package com.zhiyuan.pojo;

public class Order {
	private int order_id;
	private int user_id;
	private String order_no;
	private float money;
	private User users;
	public int getOrder_id() {
		return order_id;
	}
	public void setOrder_id(int order_id) {
		this.order_id = order_id;
	}
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}
	public String getOrder_no() {
		return order_no;
	}
	public void setOrder_no(String order_no) {
		this.order_no = order_no;
	}
	public float getMoney() {
		return money;
	}
	public void setMoney(float money) {
		this.money = money;
	}
	public User getUsers() {
		return users;
	}
	public void setUsers(User users) {
		this.users = users;
	}
	
}

第五步:创建Mybatis主配置文件,Mybatis与Spring关联不需要在Mybatis主配置文件中配置JDBC

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
	<typeAliases>
		<typeAlias type="com.zhiyuan.pojo.User" alias="user"/>
		<typeAlias type="com.zhiyuan.pojo.Order" alias="order"/>
	</typeAliases>
	
	<mappers>
		<mapper resource="com/zhiyuan/mapper/userMapper.xml"/>
	</mappers>
</configuration>

第六步:创建Spring Bean.xml文件,第一个bean是配置数据库连接,第二个bean是配置Mybatis主配置文件,第三个bean是配置接口路径

<?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"
	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">
				<!-- 配置 數據庫連接-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_mybatis?characterEncoding=utf-8"></property>
	<property name="username" value="root"></property>
	<property name="password" value="liuxi"></property>
</bean>
				<!-- 配置mybatis主配置文件 -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="configLocation" value="conf.xml"></property>
</bean>
					<!-- 配置接口 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
	<property name="sqlSessionFactory" ref="SqlSessionFactory" />
	<property name="mapperInterface" value="com.zhiyuan.mapper.UserMapper"></property>
</bean>
</beans>
第七步:创建与实体类对应的映射文件,查询数据使用ResultMap,parameterType和parameterMap的区别是parameterType是指已有基本数据类型,而parameterMap是指自定义类型

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.mapper.UserMapper">
  	<resultMap type="User" id="resultUserMap">
  		<id property="id" column="id"/>
  		<result property="username" column="username"/>
  		<result property="mobile" column="mobile"/>
  	</resultMap>
  	
  	<resultMap type="Order" id="resultOrderMap">
  		<id property="order_id" column="order_id"/>
  		<result property="user_id" column="user_id"/>
  		<result property="order_no" column="order_no"/>
  		<result property="money" column="money"/>
  		<association property="users" javaType="User">
  			<id property="id" column="id"/>
  			<result property="username" column="username"/>
  		<result property="mobile" column="mobile"/>
  		</association>
  	</resultMap>
  	
  	<select id="getUserOrder" resultMap="resultOrderMap" parameterType="int">
  	select u.*,o.* from `user` u,`order` o where u.id=o.user_id and u.id=#{id};
  	</select>
  	
  	<select id="getUserById" resultMap="resultUserMap" parameterType="int">
  		select * from user where id=#{id};
  	</select>
</mapper>

第八步:创建与映射文件对应的接口
package com.zhiyuan.mapper;

import java.util.List;

import com.zhiyuan.pojo.Order;
import com.zhiyuan.pojo.User;

public interface UserMapper {
	public User getUserById(int id);
	public List<Order> getUserOrder(int id);
}

第九步:开始测试

package com.zhiyuan.test;

import java.util.List;

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

import com.zhiyuan.mapper.UserMapper;
import com.zhiyuan.pojo.Order;
import com.zhiyuan.pojo.User;

public class Test {
	private static ApplicationContext context;
	static{
		context=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	public static void main(String[] args) {
	
		UserMapper iuser=(UserMapper) context.getBean("userMapper");
		User user=iuser.getUserById(1);
		System.out.println("獲取用戶ID為1的用戶名:"+user.getUsername());
		
		System.out.println("得到用戶ID為1的所有訂單列表");
		System.out.println("————————————————————————");
		List<Order> orders=iuser.getUserOrder(1);
		for (Order order:orders) {
			System.out.println("訂單號:"+order.getOrder_no()+"訂單金額:"+order.getMoney());
		}
	}

}
第十步:测试成功得出结果
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
獲取用戶ID為1的用戶名:yiibai
得到用戶ID為1的所有訂單列表
————————————————————————
訂單號:1509289090訂單金額:99.9
訂單號:1519289091訂單金額:290.8
訂單號:1509294321訂單金額:919.9
訂單號:1601232190訂單金額:329.9
訂單號:1503457384訂單金額:321.0
訂單號:1598572382訂單金額:342.0
訂單號:1500845727訂單金額:458.0
訂單號:1508458923訂單金額:1200.0
訂單號:1504538293訂單金額:2109.0
訂單號:1932428723訂單金額:5888.0
訂單號:2390423712訂單金額:3219.0
訂單號:4587923992訂單金額:123.0
訂單號:4095378812訂單金額:421.0
訂單號:9423890127訂單金額:678.0
訂單號:7859213249訂單金額:7689.0
訂單號:4598450230訂單金額:909.2


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值