本文给大家介绍下Spring整合Mybatis的步骤
创建一个java项目即可
<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.0modelVersion>
<groupId>com.dpbgroupId>
<artifactId>mybatis11-SSartifactId>
<version>0.0.1-SNAPSHOTversion>
<properties>
<spirng.version>4.3.3.RELEASEspirng.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spirng.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-ormartifactId>
<version>${spirng.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.27version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.25version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
dependency>
dependencies>
project>
<configuration>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
settings>
configuration>
<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.dpb.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>
<bean class="org.mybatis.spring.SqlSessionFactoryBean"
id="sqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.dpb.bean"/>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="com.dpb.mapper"/>
bean>
beans>
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
names=root
password=123456
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=C:\\tools\\logs\\dpb.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
package com.dpb.bean;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
public interface UserMapper {
public List<User> query();
}
<mapper namespace="com.dpb.mapper.UserMapper">
<select id="query" resultType="user">
select * from t_user
select>
mapper>
/**
* 接口 service
* @author dengp
*
*/
public interface IUserService {
public List<User> query();
}
@Service
public class UserServiceImpl implements IUserService{
@Resource
private UserMapper mapper;
@Override
public List<User> query() {
// TODO Auto-generated method stub
return mapper.query();
}
}
public class Test1 extends TestCase {
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService user = ac.getBean(IUserService.class);
List<User> list = user.query();
System.out.println("--->"+list.size());
for (User u : list) {
System.out.println(u.getId()+","+u.getName());
}
}
}
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-conf.xml"/>
<property name="dataSource" ref="dateSource"/>
<property name="typeAliasesPackage" value="com.sxt.model"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
bean>