mybatis整合spring01

本章学习目标:
mybatis整合spring,有Mapper实现类
代码网盘:https://pan.baidu.com/s/1tKUsO-GNeuCt2kfC0OCcWQ

项目目录结构

mybatis整合spring01_第1张图片

pom.xml

<dependencies>
		<!--junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<!--spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<!--db -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.40</version>
		</dependency>
		<!--slf4j -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
		</dependency>
		<!--mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.4</version>
		</dependency>
		<!--Mybatis Spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.4</version>
		</dependency>
	</dependencies>

User

	private String username;
	private Date birthday;
	private String sex;
	private String addres;

UserMapper

public interface UserMapper {
	// 添加客户
	public void saveUser(User user);
}

UserMapperImpl

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper {
	public void saveUser(User user) {
		SqlSession sqlSession = this.getSqlSession();
		sqlSession.insert("saveUserMapper", user); // saveUserMapper为UserMapper.xml文件中的id
		//这里不需要事务的提交	
	}
}

jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 读取jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 创建DataSource -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 配置连接池属性 -->
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 创建SqlSessionFactory对象 -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 关联连接池 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载sql映射文件 -->
		<property name="mapperLocations"
			value="classpath:mapper/*.xml"></property>
	</bean>

	<!-- 创建UserMapperImpl对象,注入sqlsessionFactory -->
	<bean id="userMapper" class="com.imooc.dao.impl.UserMapperImpl">
		<!-- 注入sqlsessionFactory -->
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

</beans>

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.imooc.dao.UserMapper"> //namespace为接口的完全限定名
	<!-- 添加用户 -->
	<insert id="saveUser" parameterType="com.imooc.po.User">
		INSERT INTO `mybatis`.`user` (
		  `username`,`birthday`,`sex`, `address`)
		VALUES
		  ( #{username},#{birthday},#{sex}, #{addres});
	</insert>
</mapper>

MybatisSpringTest

@Test
	public void test01(){
		//1、加载spring配置文件
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2、获取对象
		UserMapper userMapper = (UserMapper)ac.getBean("userMapper");
		
		// 创建user对象
		User user = new User();
		user.setAddres("北京");
		user.setBirthday(new Date());
		user.setSex("女");
		user.setUsername("杨幂3");
		
		userMapper.saveUser(user);
	}

你可能感兴趣的:(#,SSM框架,java)