mybatis和spring整合

文章目录

  • 创建maven项目
  • 引入相关依赖
  • 创建对应的配置文件
    • mybatis的全局配置文件
    • spring配置文件
    • db.properties
    • log4j.properties
  • 业务测试
    • User.java
    • mapper接口和映射文件
    • service接口和实现类
    • 测试

创建maven项目

mybatis和spring整合_第1张图片

引入相关依赖

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sxt</groupId>
  <artifactId>mybatis-17-spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
 	 <!-- Spring相关jar包 -->
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>4.3.21.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-orm</artifactId>
  		<version>4.3.21.RELEASE</version>
  	</dependency>
  	<!-- mybatis依赖 -->
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.4.6</version>
  	</dependency>
	<!-- mybatis和spring整合包 -->
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.3.2</version>
	</dependency>
	<!-- mysql驱动包 -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.27</version>
	</dependency>
	<!-- 日志框架 -->
	<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>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
	<!-- c3p0 -->
	<dependency>
		<groupId>com.mchange</groupId>
		<artifactId>c3p0</artifactId>
		<version>0.9.5.2</version>
	</dependency>
	
  </dependencies>
</project>

创建对应的配置文件

mybatis的全局配置文件

mybatis-config.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>
	
	<settings>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<setting name="aggressiveLazyLoading" value="false"/>
	</settings>
	
</configuration>

spring配置文件

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	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-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	<!-- 关联数据属性文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.service.impl"/>

	<!-- 配置数据源 -->
	<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" 
	id="dataSource" >
		<property name="driverClass" value="${driver}"></property>
		<property name="jdbcUrl" value="${url}"></property>
		<property name="user" value="${names}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	<!-- 整合mybatis -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean" 
	id="sqlSessionFactoryBean" >
		<!-- 关联数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 关联mybatis的配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"/>
		<!-- 添加别名设置 -->
		<property name="typeAliasesPackage" value="com.sxt.pojo"/>
	</bean>
	<!-- 配置扫描的路径 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
		<property name="basePackage" value="com.sxt.mapper"/>
	</bean>
</beans>

db.properties

# mysql \u5F00\u53D1\u73AF\u5883
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/1227?characterEncoding=utf-8
names=root
password=1999

# mysql \u6D4B\u8BD5\u73AF\u5883
# driver=com.mysql.jdbc.Driver
# url=jdbc:mysql://192.168.0.131:3306/1227?characterEncoding=utf-8
# username=root
# password=1999

# mysql \u751F\u4EA7\u73AF\u5883
# driver=com.mysql.jdbc.Driver
# url=jdbc:mysql://192.168.0.131:3306/1227?characterEncoding=utf-8
# username=root
# password=1999

log4j.properties

log4j.rootCategory=DEBUG, stdout , R
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
 
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\Tomcat 5.5\\logs\\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

业务测试

User.java

package com.sxt.pojo;

public class User {

	private Integer id;
	
	private String username;
	
	private String password;
	
	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 getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
	}
	
}

mapper接口和映射文件

==UserMapper ==

package com.sxt.mapper;

import java.util.List;

import com.sxt.pojo.User;

public interface UserMapper {

	public List<User> query();
}

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.sxt.mapper.UserMapper">
	<select id="query" resultType="User">
		select * from t_user
	</select>
</mapper>

service接口和实现类

==IUserService ==

package com.sxt.service;

import java.util.List;

import com.sxt.pojo.User;

public interface IUserService {

	public List<User> query();
}

UserServiceImpl

package com.sxt.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.sxt.mapper.UserMapper;
import com.sxt.pojo.User;
import com.sxt.service.IUserService;

@Service
public class UserServiceImpl implements IUserService{

	@Resource //获取代理对象 session.getMapper(UserMapper.class);
	private UserMapper mapper;
	@Override
	public List<User> query() {
		// TODO Auto-generated method stub
		return mapper.query();
	}

}

测试

package com.sxt.test;

import java.util.List;

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

import com.sxt.pojo.User;
import com.sxt.service.IUserService;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		IUserService service = ac.getBean(IUserService.class);
		List<User> list = service.query();
		for (User user : list) {
			System.out.println(user);
		}
	}
}

mybatis和spring整合_第2张图片
注意如果接口映射文件和接口文件不在同一个包下可以如下配置:

<!-- 整合mybatis -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 关联mybatis的全局配置文件 -->
	<property name="configLocation" value="classpath:mybatis-conf.xml"/>
	<!-- 关联数据源 -->
	<property name="dataSource" ref="dateSource"/>
	<!--package取别名 -->
	<property name="typeAliasesPackage" value="com.sxt.model"/>
	<!-- 映射文件和接口文件不在同一个目录下的时候 -->
	<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>

你可能感兴趣的:(mybatis,mybatis)