一个简单的spring+mybatis的项目:
项目结构
bean.UserBean.java
package bean;
public class UserBean {
private int id;
private String username;
private String password;
private String mail;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "UserBean [id=" + id + ", username=" + username + ", password="
+ password + ", mail=" + mail + ", phone=" + phone + "]";
}
public UserBean() {
super();
// TODO Auto-generated constructor stub
}
public UserBean(int id, String username, String password, String mail,
String phone) {
super();
this.id = id;
this.username = username;
this.password = password;
this.mail = mail;
this.phone = phone;
}
}
mapper.UserMapper.java
package mapper;
import java.util.List;
import bean.UserBean;
public interface UserMapper {
void save(UserBean user);
void update(UserBean user);
void delete(int id);
UserBean findById(int id);
List<UserBean> findAll();
}
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="mapper.UserMapper">
<resultMap type="UserBean" id="userResult">
<result column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="mail" property="mail"/>
<result column="phone" property="phone"/>
</resultMap>
<!-- 取得插入数据后的id -->
<insert id="save" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
insert into usertable2(username,password,mail,phone)
values(#{username},#{password},#{mail},#{phone})
</insert>
<update id="update">
update usertable2
set username = #{username},
password = #{password},
mail = #{mail},
phone = #{phone}
where id = #{id}
</update>
<delete id="delete">
delete from usertable2
where id = #{id}
</delete>
<select id="findById" resultMap="userResult">
select *
from usertable2
where id = #{id}
</select>
<select id="findAll" resultMap="userResult">
select *
from usertable2
</select>
</mapper>
beans.xml
一定注意这个配置文件中的包名的value
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource
数据源中的url有时候会发生useSSL错误,这个时候需要在&后加上amp;
-->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--
2. mybatis的SqlSession的工厂: SqlSessionFactoryBean
dataSource / typeAliasesPackage
第二个property的value的值对应的是实体类的包名
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="bean"/>
</bean>
<!--
3. mybatis自动扫描加载Sql映射文件 : MapperScannerConfigurer
sqlSessionFactory / basePackage
其中的value是对应的接口、**Mapper.xml所在包
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="mapper"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
测试类:
test.SMtest.java
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import bean.UserBean;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/beans.xml")//加载spring配置文件beans.xml
public class SMtest {
@Autowired
private UserMapper userMapper;
@Test
public void testadd(){
UserBean user = new UserBean(-1, "tom", "12145", "32r@qq.com", "11111111111");
userMapper.save(user);
System.out.println("testadd..");
}
@Test
public void testupdate(){
UserBean user = new UserBean(5, "tom", "11111", "32r@qq.com", "11111111111");
userMapper.update(user);
System.out.println("testupdate..");
}
@Test
public void testdelete(){
userMapper.delete(1);
System.out.println("testdelete..");
}
@Test
public void testfindById(){
UserBean user1 = new UserBean();
user1=userMapper.findById(2);
System.out.println(user1.getUsername());
System.out.println("testFindById..");
}
@Test
public void testfindAll(){
List<UserBean> list = new ArrayList<UserBean>();
list=userMapper.findAll();
/*for(UserBean user:list){ //用for遍历list
System.out.println(user.getUsername()+user.getMail());
}*/
Iterator it = list.iterator();//iterator遍历list
while(it.hasNext()){
System.out.println(it.next());
}
System.out.println("testfindAll..");
}
}