整合步骤:
1、引入依赖或添加jar包
2、配置数据源db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF8
3、配置Spring的applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置扫描的包 -->
<context:component-scan base-package="com.dbm.mybatis"/>
<context:property-placeholder location="classpath:db.properties" />
<!-- 使用c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="driverClass" value="${jdbc.driverClass}" />
</bean>
<!-- 配置mybatis的sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 有了该配置就可以使用简单类名 -->
<property name="typeAliasesPackage" value="com.atguigu.mybatis"></property>
<!-- 核心配置文件位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置Mapper所在的package -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bdm.mybatis"></property>
</bean>
</beans>
4、新建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>
</configuration>
5、新建Entity + Mapper接口或XML配置 + Service
public class User implements Serializable{
private Integer id;
private String name;
private int age;
}
public interface UserMapper{
public void add(User user);
}
<?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.bdm.mybatis.mapper.UserMapper">
<insert id="add" parameterType="User">
insert into tbl_user(name,age) values(#{name},#{age})
</insert>
<select id="getUserById" parameterType="Integer" resultType="User">
select * from tbl_user where id=#{id}
</select>
</mapper>
注意:spring3.0以下不支持mybatis
Tip
:MyBatis还提供了逆向工程的功能,可以根据表结构生成对应的Entity、Mapper接口和Mapper.xml文件,具体参考:MyBatis的逆向工程