本人使用的是MyBatis3.2.2这个版本首先在项目中导入mybatis3.2.2的jar包和mybatis-spring-1.2.1.jar包以及jdbc和spring的jar和相关jar包:
创建实体类:
package com.tc.spring.mybatis.demo.model;
public class Book {
private Integer id;
private String title;
private String author;
private Float price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public Book() {
}
public Book(Integer id, String title, String author, Float price) {
super();
this.id = id;
this.title = title;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", author=" + author
+ ", price=" + price + "]";
}
}
创建数据访问接口:
package com.tc.spring.mybatis.demo.dao;
import java.util.List;
import com.tc.spring.mybatis.demo.model.Book;
public interface IBookDao {
List<Book> list();
void save(Book book);
void update(Book book);
void delete(Book book);
Book findById(Integer id);
}
在实体类的包下添加mybatis的实体sql-map文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.tc.spring.mybatis.demo.dao.IBookDao">
<select id="list" resultType="Book">
select * from book
</select>
<select id="findById" parameterType="int" resultType="Book">
select * from book where id=#{id}
</select>
<insert id="save" parameterType="Book">
insert into book(title,author,price) values(#{title},#{author},#{price})
</insert>
<delete id="delete" parameterType="int">
delete book where id=#{id}
</delete>
<update id="update" parameterType="Book">
update book set title=#{title},author=#{author},price=#{price} where id=#{id}
</update>
</mapper>
在src下添加beans.xml文件和mybatis的配置文件
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://localhost:1433;DataBaseName=tempdb" />
<property name="username" value="sa" />
<property name="password" value="sa" />
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
<bean id="bookDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.tc.spring.mybatis.demo.dao.IBookDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration SYSTEM "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<package name="com.tc.spring.mybatis.demo.model"/>
</typeAliases>
<mappers>
<mapper resource="com/tc/spring/mybatis/demo/model/Book-sql.xml"/>
</mappers>
</configuration>
测试代码:
package com.tc.spring.mybatis.demo.test;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.tc.spring.mybatis.demo.dao.IBookDao;
import com.tc.spring.mybatis.demo.model.Book;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")
public class MyTest {
@Resource
private IBookDao bookDao;
@Test
public void test() {
Book book=new Book(1, "struts2", "tonysong", 123.33f);
this.bookDao.save(book);
}
@Test
public void list(){
List<Book> list=this.bookDao.list();
for(Book b: list){
System.out.println(b);
}
}
}