Mybatis数据的增删改查

2016-10-18 21:05    822人浏览    标签:    

数据:

Student{id int,name String ,age int}

配置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>
	
	<properties>
		<property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
		<property name="jdbc.username" value="root"/>
		<property name="jdbc.password" value="123456"/>
	</properties>
	<!-- <typeAliases>
		<typeAlias alias="Student" type="com.qinb.model.Student"/>
	</typeAliases> -->
	<typeAliases>
		<package name="com.qinb.model"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
		<environment id="test">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${jdbc.driverClassName}" />
				<property name="url" value="${jdbc.url}" />
				<property name="username" value="${jdbc.username}" />
				<property name="password" value="${jdbc.password}" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<!-- <mapper resource="com/qinb/mappers/StudentMapper.xml" /> -->
		<!-- <mapper class="com.qinb.mappers.StudentMapper"/> -->
		<package name="com.qinb.mappers"/>
	</mappers>
</configuration>

工厂:

package com.qinb.util;
 
import java.io.InputStream;
 
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
public class SqlSessionFactoryUtil {
 
	private static SqlSessionFactory sqlSessionFactory;
	
	public static SqlSessionFactory getSqlSessionFactory(){
		if(sqlSessionFactory==null){
			InputStream inputStream=null;
			try{
				inputStream=Resources.getResourceAsStream("mybatis-config.xml");
				sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return sqlSessionFactory;
	}
	
	public static SqlSession openSession(){
		return getSqlSessionFactory().openSession();
	}
}

Dao接口

package com.qinb.mappers;
 
import java.util.List;
 
import com.qinb.model.Student;
 
public interface StudentMapper {
 
	public int add(Student student);
	
	public int update(Student student);
	
	public int delete(Integer id);
	
	public Student findById(Integer id);
	
	public List<Student> list();
}

Mapper.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="com.qinb.mappers.StudentMapper">
	
	<resultMap type="Student" id="StudentResult">
		<id property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="age" column="age"/>
	</resultMap>
	
	<insert id="add" parameterType="Student"  >
		insert into t_student values(null,#{name},#{age})
	</insert>
	<update id="update" parameterType="Student" >
		update t_student set name=#{name},age=#{age} where id=#{id}
	</update>
	
	<delete id="delete" parameterType="Integer">
		delete from t_student where id=#{id}
	</delete>
	
	<select id="findById" parameterType="Integer" resultType="Student">
		select * from t_student where id=#{id}
	</select>
	
	<select id="list" resultMap="StudentResult">
		select * from t_student
	</select>
</mapper>

测试类:

package com.qinb.service;
 
import static org.junit.Assert.fail;
 
import java.util.List;
import java.util.logging.Logger;
 
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import com.qinb.mappers.StudentMapper;
import com.qinb.model.Student;
import com.qinb.util.SqlSessionFactoryUtil;
 
public class StudentTest2 {
 
	private static Logger logger=Logger.getLogger(StudentTest2.class.getName());
	private SqlSession sqlSession=null;
	private StudentMapper studentMapper= null;
	
	@Before
	public void setUp() throws Exception {
		sqlSession = SqlSessionFactoryUtil.openSession();
		studentMapper=sqlSession.getMapper(StudentMapper.class);
	}
 
	@After
	public void tearDown() throws Exception {
		sqlSession.close();
	}
	@Test
	public void testAdd(){
		logger.info("添加学生");
		Student student = new Student("王五",20);
		studentMapper.add(student);
		sqlSession.commit();
	}
	@Test
	public void testUpdate(){
		logger.info("更新学生");
		Student student = new Student(5,"秦豹2",22);
		studentMapper.update(student);
		sqlSession.commit();
	}
	@Test
	public void testDelete(){
		logger.info("删除学生");
		studentMapper.delete(2);
		sqlSession.commit();
	}
	@Test
	public void testFindById(){
		logger.info("根据id获取学生");
		Student student =studentMapper.findById(5);
		System.out.println(student);
		sqlSession.commit();
		//查询可以不用提交事物
	}
	@Test
	public void testList(){
		logger.info("获取所有学生");
		List<Student> studentList = studentMapper.list();
		for(Student stu:studentList){
			System.out.println(stu);
		}
	}
	
	@Test
	public void test() {
		fail("Not yet implemented");
	}
 
}

项目:

blob.png

相关文章

留言评论

评论已关闭。