一、运行环境
MyEclipse+Tomcat+MySQL
二、项目包结构
三、entity实体包
<span style="font-size:14px;">package com.tccp.entity;
/**
* 用户
* @author tccp项目组
*
*/
public class User {
private int user_id;
private String username;
private String age;
public User(){
super();
}
public User(int user_id, String username, String age) {
super();
this.user_id = user_id;
this.username = username;
this.age = age;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [user_id=" + user_id + ", username=" + username +
", age=" + age + "]";
}
}</span><span style="font-size: 18px;">
</span>
三、persistence持久层
接口类:
<span style="font-size:14px;">package com.tccp.persistence;
import java.util.List;
import com.tccp.entity.User;
public interface UserMapper {
//增删改查
void add(User user);
boolean update(User user);
boolean delete(int user_id);
User findById(int user_id);
List<User> findAll();
}
</span>
xml配置文件:
<span style="font-size:14px;"><?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">
<!--
namespace:必须与对应的接口全类名一致
id:必须与对应接口的某个对应的方法名一致
-->
<mapper namespace="com.tccp.persistence.UserMapper">
<!-- mybsits_config中配置的alias类别名,也可直接配置resultType为类路径 -->
<insert id="add" parameterType="User">
insert into user(username, age) values(#{username},#{age})
</insert>
<update id="update" parameterType="User">
update user set username=#{username},age=#{age} where user_id=#{user_id}
</update>
<delete id="delete" parameterType="int">
delete from user where user_id=#{user_id}
</delete>
<select id="findById" parameterType="int" resultType="User">
select user_id user_id,username username,age age from user where user_id=#{user_id}
</select>
<select id="findAll" resultType="User">
select user_id user_id,username username,age age from user
</select>
</mapper></span>
四、数据库操作测试test
spring测试:
<span style="font-size:14px;">package com.tccp.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("config/spring-common.xml");
Object userMapper = ctx.getBean("userMapper");
System.out.println(userMapper);
}
}</span><span style="font-size: 18px;">
</span>
数据库操作测试:
<span style="font-size:14px;">package com.tccp.test;
import java.util.List;
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 com.tccp.persistence.UserMapper;
import com.tccp.entity.User;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/config/spring-common.xml")
public class UserTest {
@Autowired
private UserMapper userMapper;
@Test
public void testAdd(){
User user = new User(4, "王五", "33");
userMapper.add(user);
}
@Test
public void testFindAll(){
List<User> findAllList = userMapper.findAll();
System.out.println(findAllList.size());
}
@Test
public void testFindById(){
User user = userMapper.findById(2);
System.out.println(user.getUser_id());
System.out.println(user.getUsername());
}
@Test
public void testUpdate(){
User user = new User(5, "钱6", "24");
userMapper.update(user);
}
@Test
public void testDelete(){
userMapper.delete(4);
}
}
</span>
(配置文件与业务逻辑层代码略)