maven创建web项目,架构如下图
maven引用如下图,其中jdbc-driver由于版权问题在maven仓库找不到,是我自己本地注册的。
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: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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<description>Spring公共配置文件 </description>
<context:component-scan base-package="org.wusq.project" />
<context:annotation-config/>
<context:property-placeholder location="classpath:application.properties" />
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close" lazy-init="false">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation">
<value>classpath:mybatis-config.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.wusq.project" />
<property name="annotationClass" value="org.wusq.project.dao.Mapper"></property>
</bean>
</beans>
dao包中的两个接口如下
package org.wusq.project.dao;
import java.io.Serializable;
public interface Dao<T> {
int delete(Serializable id);
int insert(T o);
int update(T o);
}
package org.wusq.project.dao;
public @interface Mapper {
}
user包中,User实体类,两个属性id和name,省略代码。其他代码如下
package org.wusq.project.user;
import org.wusq.project.dao.Dao;
import org.wusq.project.dao.Mapper;
@Mapper
public interface UserMapper extends Dao<User>{
User selectByName(String name);
}
<?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="org.wusq.project.user.UserMapper">
<insert id="insert" parameterType="User">
<![CDATA[
insert into users (id,name)
values
(#{id,jdbcType=VARCHAR},#{name,jdbcType=VARCHAR})
]]>
</insert>
<select id="selectByName" parameterType="String" resultType="User">
select id,name from users where name=#{name}
</select>
</mapper>
package org.wusq.project.user;
public interface UserService {
void save(User o);
User queryByName(String id);
}
package org.wusq.project.user;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Resource
private UserMapper userMapper;
@Override
public void save(User o) {
userMapper.insert(o);
}
@Override
public User queryByName(String name) {
return userMapper.selectByName(name);
}
}
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>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias type="org.wusq.project.user.User" alias="User"/>
</typeAliases>
</configuration>
测试类如下
package test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.wusq.project.user.User;
import org.wusq.project.user.UserService;
public class ServiceTest {
private static ApplicationContext act = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
act = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void test() throws Exception{
UserService service=(UserService)act.getBean("userServiceImpl");
User o = new User();
o.setId("1");
o.setName("wusq");
service.save(o);
// User u = service.queryByName("wusq");
// System.out.println(u.getName());
}
}
Mybatis+Spring基于接口编程,核心dao只有一个接口,即该例中的Dao,里面可以写一些通用接口。然后面向对象编程中,可以在实体Mapper中扩展自用接口。service中调用dao层接口时,由mybatis代理实现。