mybatis与spring的整合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis与spring的整合相关的知识,希望对你有一定的参考价值。
spring 框架
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
两个重要模块:Spring 面向方面编程(AOP)和控制反转 (IOC) 容器。
控制反转模式(也称作依赖性介入)的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
容器 (在 Spring 框架中是 IOC 容器) 负责将这些联系在一起。在典型的 IOC 场景中,容器创建了所有对象,并设置必要的属性将它们连接在一起,决定什么时间调用方法
mybatis框架
MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。
MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
1.项目结构
POJO-----User.java
package com.it.ms.pojo; import java.io.Serializable; import java.util.Date; public class User implements Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address + "]"; } }
pojo----Order
package com.it.ms.pojo; import java.io.Serializable; import java.util.Date; public class Orders implements Serializable{ /** * */ private static final long serialVersionUID = 1L; private Integer id; private Integer userId; private String number; private Date createtime; private String note; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number == null ? null : number.trim(); } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public String getNote() { return note; } public void setNote(String note) { this.note = note == null ? null : note.trim(); } @Override public String toString() { return "Orders [id=" + id + ", userId=" + userId + ", number=" + number + ", createtime=" + createtime + ", note=" + note + "]"; } }
DA0---UserMapper
package com.it.ms.mapper; import com.it.ms.pojo.User; public interface UserMapper { //根据ID查用户 public User findById(Integer id); }
UserMapper.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.it.ms.mapper.UserMapper"> <select id="findById" parameterType="Integer" resultType="com.it.ms.pojo.User"> select * from user where id = #{id}; </select> </mapper>
DAO---OrderMapper.java
package com.it.ms.mapper; public interface OrderMapper { }
OrderMapper.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.it.ms.mapper.OrderMapper"> </mapper>
记载文件db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/matbatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=0821
Spring配置文件 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="10"/> <property name="maxIdle" value="5"/> </bean> <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 配置mybatis核心文件 --> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置MapperScannerConfigurer --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.it.ms.mapper"></property> </bean> </beans>
mybatis配置文件 sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="com/it/ms/mapper/UserMapper.xml"/> <mapper resource="com/it/ms/mapper/OrderMapper.xml"/> </mappers> </configuration>
注意:因为在spring配置文件中使用的是扫描包的方式来寻找mapper映射文件,所以在mybatis中需要配置两个映射文件,即使在这里没有用到OrderMapper。当然,在spring配置文件中扫描基本包了。在这里的mapper配置是可以一起去掉的,但是不能只写一个,会报错。
Test
package com.it.ms.test; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.it.ms.mapper.UserMapper; import com.it.ms.pojo.User; public class UserMapperTest { private ApplicationContext context; @Before public void setUp() throws Exception{ context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } @Test public void testFindById(){ //获取Mapper UserMapper userMapper = (UserMapper) context.getBean("userMapper"); User user = userMapper.findById(1); System.out.println(user); } }
以上是关于mybatis与spring的整合的主要内容,如果未能解决你的问题,请参考以下文章