1.在创建好的工程中导入架包
(1)spring需要的包
IOC架包
commons-logging-1.1.1.jar
spring-beans-4.1.3.RELEASE.jar
spring-context-4.1.3.RELEASE.jar
spring-core-4.1.3.RELEASE.jar
spring-expression-4.1.3.RELEASE.jar
AOP架包
aspectjweaver-1.6.11.jar
spring-aop-4.1.3.RELEASE.jar
spring-aspects-4.1.3.RELEASE.jar
mybatis架包
mybatis-3.2.7.jar
Spring整合mybatis需要的jar包
commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
mybatis-spring-1.2.2.jar
spring-tx-4.1.3.RELEASE.jar
其他的架包
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mysql-connector-java-8.0.11.jar及其依赖包
(2) 编写配置文件
mybatis配置文件
<?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>
<!-- bean下的包自动起别名, -->
<typeAliases>
<package name="com.zpark.bean"/>
</typeAliases>
<!-- 包扫描映射 -->
<mappers>
<package name="com.zpark.mapper"/>
</mappers>
</configuration>
Spring配置文件
<?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">
<!-- 加载 jdbc.properties -->
<context:property-placeholder
location="classpath:jdbc.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="${jdbc.dirverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的类 -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
</bean>
<!-- 配置mapper代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.zpark.mapper.UserMapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
jdbc.properties
jdbc.dirverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=GMT
jdbc.username=root
jdbc.password=root
(3)实体类
package com.zpark.bean;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String usernmae;
private Date birthday;
private String sex;
private String address;
private int password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsernmae() {
return usernmae;
}
public void setUsernmae(String usernmae) {
this.usernmae = usernmae;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", usernmae=" + usernmae + ", birthday=" + birthday + ", sex=" + sex + ", address="
+ address + ", password=" + password + "]";
}
}
(4)编写接口使用mybatis动态代理
UserMapper.java
package com.zpark.mapper;
import com.zpark.bean.User;
public interface UserMapper {
public User findUserById(Integer id);
}
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.zpark.mapper.UserMapper">
<select id="findUserById" parameterType="java.lang.Integer" resultType="User">
select * from user where id = #{id}
</select>
</mapper>
(5)测试
package com.zpark.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zpark.bean.User;
import com.zpark.mapper.UserMapper;
import com.zpark.mapper.UserMapper;
public class TestSM {
private ApplicationContext applicationContext;
@Before
public void setUp() throws Exception{
//初始化spring运行环境
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void testFindUserById() throws Exception {
UserMapper userMapper = applicationContext.getBean(UserMapper.class);
User user = userMapper.findUserById(1);
System.out.println(user);
}
}
(6)测试结果
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
User [id=1, usernmae=null, birthday=null, sex=2, address=null, password=1]