导入jar包
新建一个java project,命名为mybatis-spring.
在项目下新建一个lib文件夹,导入相应的包:
导入后,选中lib下的所有jar包,右键——>Build Path——>Add to BuildPath.
src下新建sqlMapConfig.xml
sqlMapConfig.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>
<!-- 设置别名 -->
<typeAliases>
<!-- 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.zrxjuly.mybatis.pojo" />
</typeAliases>
<mappers>
<!-- 扫描mapper下的xml文件 -->
<package name="com.zrxjuly.mybatis.mapper"/>
</mappers>
</configuration>
src下新建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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载数据库配置文件. -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 此处的property的name值不可随便更改,必须是以下名称 -->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- mybatis的工厂. 此处的class名直接从 mybatis-spring-xxx.jar下的org.mybatis.spring下拷贝SqpSessionFactoryBean的类名称-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- mybatis核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
</bean>
<!-- 方式1:手动实例化DAO,原始的开发 -->
<bean id="userDAO" class="com.zrxjuly.mybatis.dao.UserDAOImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
</bean>
<!-- 方式2:使用Mapper动态代理开发 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
注入mybatis的工厂
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="mapperInterface" value="com.zrxjuly.mybatis.mapper.UserMapper"></property>
</bean> -->
<!-- 方式3:扫描 Mapper动态代理开发 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包 扫描的是基础包下的类文件.-->
<property name="basePackage" value="com.zrxjuly.mybatis.mapper"></property>
</bean>
</beans>
src下的db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=rongxiang
src下的log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
User.java
src下新建包,名为com.zrxjuly.mybatis.pojo,在该包下新建User.java
package com.zrxjuly.mybatis.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String sex;
private Date birthday;
private String address;
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + "]";
}
}
UserMapper.java
src下新建包,名为com.zrxjuly.mybatis.mapper,包下新建UserMapper.java。
package com.zrxjuly.mybatis.mapper;
import com.zrxjuly.mybatis.pojo.User;
public interface UserMapper {
public User selectUserById(Integer id);
}
UserMapper.xml
与UserMapper.java同一个目录下,新建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.zrxjuly.mybatis.mapper.UserMapper">
<select id="selectUserById" parameterType="Integer" resultType="User">
select * from user where id=#{id}
</select>
</mapper>
新建测试类
src下新建包,名为com.zrxjuly.mybatis.junit,在包下新建UserTest.java
package com.zrxjuly.mybatis.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zrxjuly.mybatis.mapper.UserMapper;
import com.zrxjuly.mybatis.pojo.User;
/**
*
* 测试类.
*
*/
public class UserTest {
// 使用Mapper动态代理开发applicationContext.xml中的方式2.
@SuppressWarnings("resource")
@Test
public void testMapper() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ac.getBean("userMapper");
User user = userMapper.selectUserById(1);
System.out.println(user);
}
// 使用Mapper动态代理开发applicationContext.xml中的方式3.
@SuppressWarnings("resource")
@Test
public void testMappers() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper userMapper = (UserMapper) ac.getBean(UserMapper.class);
User user = userMapper.selectUserById(1);
System.out.println(user);
}
}