好像不管什么技术,都要跟spring方便的结合起来才能更好的使用,当然mybatis这么好的半自动框架自然也不例外,并且也很容易的将其与spring整合起来。
首先不管怎么,先编写对应的pom文件,添加对应的依赖包
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springv}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
一次整合不齐也没什么关系,当测试的时候,再根据对应的错误提醒添加对应的jar包即可.
既然是和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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${user}"></property>
<property name="password" value="${passwd}"></property>
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlMapperConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务相关控制 -->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="userTxAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="select*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.java.service.*.*(..))" />
<!--把事务控制在Service层-->
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>
<!-- dao -->
<bean id="personDao" class="com.java.dao.impl.PersonDaoImpl">
<property name="sqlSessionFactory" ref="sessionFactory"></property>
</bean>
<!-- service -->
<bean id="personService" class="com.java.service.impl.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
配置的东西都有注释,就不解释了.
接下来当然就是 sqlMapperConfig.xml与bean的映射文件了.这些文件都可以直接从官网文档上拿过来直接修改.
sqlMapperConfig.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>
<typeAlias type="com.java.bean.Person" alias="Person"/>
</typeAliases>
<mappers>
<mapper resource="com/java/bean/Person.xml"/>
</mappers>
</configuration>
persom.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="Person">
<insert id="insert" parameterType="Person" keyColumn="id" useGeneratedKeys="true">
insert into person(name,age) values(#{name},#{age})
</insert>
<update id="update" parameterType="Person">
update person set name=#{name},age=#{age} where id=#{id}
</update>
<select id="findbyid" parameterType="int" resultType="Person">
select * from person where id=#{id}
</select>
<select id="findList" resultType="Person">
select * from person
</select>
<delete id="deletebyid" >
delete from person where id=#{id};
</delete>
</mapper>
要注意命名空间哟。并且每次调用的时候也要添加上命名空间
然后依次的创建dao service 实体bean
persondaoimpl
<pre name="code" class="java">package com.java.dao.impl;
import java.util.List;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.java.bean.Person;
import com.java.dao.PersonDao;
/**
* persondao层实现类
* @author: jipeng
* @date: 2014-8-6 下午1:00:50
*/
public class PersonDaoImpl extends <span style="color:#FF0000;">SqlSessionDaoSupport</span> implements PersonDao {
public int insertPerson(Person person) {
return getSqlSession().insert("Person.insert", person);
}
public int deletePerson(Person person) {
// TODO Auto-generated method stub
return getSqlSession().delete("Person.deletebyid", person);
}
public int updatePerson(Person person) {
// TODO Auto-generated method stub
return getSqlSession().update("Person.update", person);
}
public Person findPersonById(int id) {
// TODO Auto-generated method stub
return getSqlSession().selectOne("Person.findbyid",id);
}
public List<Person> findPersonList() {
// TODO Auto-generated method stub
return getSqlSession().selectList("Person.findList");
}
}
dao 应该继承 SqlSessionDaoSupport 已方便开发.
personServiceImpl
package com.java.service.impl;
import java.util.List;
import com.java.bean.Person;
import com.java.dao.PersonDao;
import com.java.service.PersonService;
/**
* person service实现类
* @author: jipeng
* @date: 2014-8-6 下午1:02:07
*/
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public int insertPerson(Person person) {
return personDao.insertPerson(person);
}
public int deletePerson(Person person) {
return personDao.deletePerson(person);
}
public int updatePerson(Person person) {
// TODO Auto-generated method stub
return personDao.updatePerson(person);
}
public Person findPersonById(int id) {
// TODO Auto-generated method stub
return personDao.findPersonById(id);
}
public List<Person> findPersonList() {
// TODO Auto-generated method stub
return personDao.findPersonList();
}
}
接下来就是测试啦
package com.java.test;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java.bean.Person;
import com.java.service.PersonService;
public class PersonTest {
private static PersonService personService=null;
static{
@SuppressWarnings("resource")
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
personService =(PersonService) applicationContext.getBean("personService");
}
@Test
public void testInsert(){
Person person=new Person("宇智波鼬", 22);
int i=personService.insertPerson(person);
System.out.println(i);
}
@Test
public void testUppdate(){
Person person=new Person(1, "卡卡西", 20);
int i=personService.updatePerson(person);
System.out.println(i);
}
@Test
public void testfindbyid(){
Person person=personService.findPersonById(1);
System.out.println(person);
}
@Test
public void testFindList(){
List<Person> persons=personService.findPersonList();
for (Person person : persons) {
System.out.println(person);
}
}
@Test
public void testDelete(){
Person person=new Person(2);
int i=personService.deletePerson(person);
System.out.println(i);
}
}
至此完成,应该是很简单的吧