我的Mybatis入门(四)整合Spring

为什么要整合Spring

我的想法:Spring可以帮我们管理对象,方便我们开发

整合步骤

创建项目,引入jar包

创建过程略,引入jar包如下图所示:
引入jar包

基本配置
  1. 创建User.class类
public class User {
   private int id;
   private String name;
   private int age;
   //省略get set toString方法
}
  1. 引入jdbc.properties文件,编写数据库连接信息
  • 备注:需要是用jdbc.driver,避免出现org.springframework.jdbc.CannotGetJdbcConnectionException异常
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=111111
  1. 引入日志打印配置文件 log4j.properties文件
创建Mapper文件夹,配置编写Mapper.java和Mapper.xml

Mapper.java

public interface UserMapper {
   /**
    * 查找用户
    * */
   public User findUserById(Integer id);
}

Mapper.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">

<!--  namespace要写到具体文件,这个地方跟全路径扫描不一样 -->
<mapper namespace="com.syw.mapper.UserMapper">
   <select id="findUserById" parameterType="Integer" resultType="User">
   	select * from user2 where id = #{v}
   </select>
</mapper>
编写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>
   <!-- 别名 -->
   <typeAliases>
   	<package name="com.syw.bean" />
   </typeAliases>
   <!-- 配置mapper文件路径 -->
   <mappers>
   	<package name="com.syw.mapper" />
   </mappers>
</configuration>
编写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:jdbc.properties" />

	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<!-- el表达式前必须加jdbc,否则会出现org.springframework.jdbc.CannotGetJdbcConnectionException -->
		<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>

	<!-- 实例化sqlSession的工厂 -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:sqlMapConfig.xml" />
	</bean>

	<!-- Mapper动态代理开发 自动扫描basePackage下的所有文件,所以不用配置id -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 如果配置了动态代理,sqlMapConfig.xml就可以不用指定mapper的路径了,
		也就是不需要设置<mappers></mappers>了 -->
		<!-- 基本包 -->
		<property name="basePackage" value="com.syw.mapper" />
	</bean>

</beans>
编写测试类
public class TestSpringMybatis {
	
	private ClassPathXmlApplicationContext ac;
	
	@Before
	public void setUp() throws Exception {
		this.ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
	}
	/**
	 * 查询单个
	 */
	@Test
	public void selectOneMybatis() throws Exception {
		// 可以直接通过类名获取对应的mapper
		UserMapper mapper = ac.getBean(UserMapper.class);
		User user = mapper.findUserById(1);
		System.out.print(user);
	}
}
整体目录结构如下图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值