Fork me on GitHub

【mybatis】学习笔记4 Spring+ mybatis 整合

附上学习视频黑马程序员  https://www.iqiyi.com/v_19rrad4y88.html#curid=623832100_d64bcfbae20d0fef8e67a50d3e1293b1

applicationContext.xml  配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
 
	<!-- 读取db.properties -->
	<context:property-placeholder location="db.properties"/>
	
	<!-- 配置c3p0连接池 -->
	<bean name ="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=UTC"/>
		<property name="user" value="root"/>
		<property name="password" value="88888888"/>
	</bean>	
 
	<!-- 配置mybatis sqlSessionFactory -->
	<bean name="sqlSessionFactoryBean"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 告诉spring mybatis的核心配置文件在哪 -->
		<property name="configLocation"
			value="classpath:sqlMapconfig.xml" />
	</bean>
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.mapper"/>
	</bean>
</beans>

sqlMapconfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
		<!-- 配置别名 -->
	<typeAliases>
		<package name="com.bean"/>
	</typeAliases>
	
	<mappers>
		<!-- <mapper resource="com/sikiedu/mapper/UserMapper.xml"/> -->
		<package name="com.mapper"/>
	</mappers>
</configuration>

test.java

package com.test;
 
import org.junit.Test;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class test1 {
@Test
public void test() {
	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	SqlSessionFactoryBean sfb = ac.getBean(SqlSessionFactoryBean.class);
	System.out.println(sfb);
}
}

c3p0的使用!!!

https://blog.csdn.net/soyuone/article/details/51554263

Spring+ mybatis DAO式开发!!!

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
 
 
	<!-- 配置c3p0连接池 数据源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=UTC" />
		<property name="user" value="root" />
		<property name="password" value="88888888" />
	</bean>
 
	<!-- 配置mybatis sqlSessionFactory -->
	<bean name="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="configLocation"
			value="classpath:sqlMapconfig.xml" />
		<property name="dataSource" ref="dataSource" />
		<!-- 告诉spring mybatis的核心配置文件在哪 -->
 
	</bean>
 
 
	<bean id="UserDaoImpl" class="com.dao.UserDaoImpl">
		<!-- 将工场注入到dao的父类中 sqlSessionFactory -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
	</bean>
</beans>

sqlMapconfig.xml

<mapper resource="com/mapper/UserMapper.xml" />   会导致报错 扫描了两遍
  <package name="com.mapper" />

https://www.cnblogs.com/hfx123/p/9685721.html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 配置别名 -->
	<typeAliases>
		<package name="com.bean" />
	</typeAliases>
 
	<mappers>
 
		<package name="com.mapper" />
	</mappers>
</configuration>

userDaoImpl

package com.dao;
 
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
 
import com.bean.User;
 
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {  //必须继承SQL sessionDaosupport
 
	@Override
	public User getUserById(Integer id) {
		SqlSession session = this.getSqlSession(); // 直接获取 即可 session 由spring注入 得写在方法内!!!
		return session.selectOne("UserMapper.selectUserById", 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="UserMapper">
 
	<!-- 根据id查询用户 -->
	<select id="selectUserById" parameterType="Integer"
		resultType="com.bean.User">
		select * from user where u_id = #{id}
	</select>
 
</mapper>

test

package com.test;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.bean.User;
import com.dao.UserDaoImpl;
 
public class UserDaoTest {
 
	@Test
	public void DaoTest() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDaoImpl userdao = (UserDaoImpl) ac.getBean("UserDaoImpl");
		User user = userdao.getUserById(1);
		System.out.println(user);
	}
}

改进mapper 动态代理开发

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.mapper.UserMapper">
 
	<!-- 根据id查询用户 -->
	<select id="selectUserById" parameterType="Integer"
		resultType="com.bean.User">
		select * from user where u_id = #{id}
	</select>
 
</mapper>

userMapper.java

package com.mapper;
 
import com.bean.User;
 
public interface UserMapper {
	public User selectUserById(Integer id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!-- 配置别名 -->
	<typeAliases>
		<package name="com.bean" />
	</typeAliases>
 
	<mappers>
		<package name="com.mapper" />
	</mappers>
</configuration>

test

package com.test;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.bean.User;
import com.mapper.UserMapper;
 
public class UserDaoTest {
 
	@Test
	public void DaoTest() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserMapper um =  (UserMapper) ac.getBean("UserMapper");
		User user = um.selectUserById(1);
		System.out.println(user);
	}
 
 
}

mapper扫描开发

package com.test;
 
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.bean.User;
import com.mapper.UserMapper;
 
public class UserDaoTest {
 
	@Test
	public void DaoTest() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserMapper um = (UserMapper) ac.getBean(UserMapper.class);
		User user = um.selectUserById(1);
		System.out.println(user);
	}
 
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:util="http://www.springframework.org/schema/util"
	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.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
 
 
	<!-- 配置c3p0连接池 数据源 -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
		<property name="jdbcUrl"
			value="jdbc:mysql://localhost:3306/ssm_mybatis?serverTimezone=UTC" />
		<property name="user" value="root" />
		<property name="password" value="88888888" />
	</bean>
 
	<!-- 配置mybatis sqlSessionFactory -->
	<bean name="sqlSessionFactoryBean"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<!-- 告诉spring mybatis的核心配置文件在哪 -->
		<property name="configLocation"
			value="classpath:sqlMapconfig.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>    
 
	<!-- mapper 动态代理 -->
	<bean name="UserMapper"
		class="org.mybatis.spring.mapper.MapperFactoryBean">
		<!-- 需要注入sqlsessionFactory -->
			<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
		<property name="mapperInterface" value="com.mapper.UserMapper" />
	</bean>
	
	<!-- mapper 扫描开发  sqlSessionFactoryBean自动结合!!! -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<property name="basePackage" value="com.mapper.UserMapper"></property>
	</bean>
</beans>
posted @ 2019-07-09 09:56  cznczai  阅读(223)  评论(0编辑  收藏  举报