一.项目结构图
二.pom.xml,数据库表的结构,Student实体类同上篇文章
三.定义StudentDao接口,定义操作数据库的方法
package cn.bd.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import cn.bd.domain.Student;
public interface StudentDao {
//查询
List<Student> getAllStudent();//查找所有学生信息
Student getStudent(int id);//查找指定id信息
List<Student> getStudentByCity(String city);//按城市查找
List<Student> getStudentByName(@Param("name")String name);
Student getStudentByIdName(@Param("id")int id,@Param("name")String name);
int addStudent(Student stu);
int deleteStudent(int id);
int update(Student stu);
}
四.在实现StudentDao接口前,需要建立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 resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClass}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper.xml" />
</mappers>
</configuration>
配置文件中引入了外部文件db.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
五.定义mapper.xml配置文件,实现StudentDao接口(底层原理,动态代理)
<?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="cn.bd.dao.StudentDao">
<select id="getAllStudent" resultType="cn.bd.domain.Student">
select * from student
</select>
<select id="getStudent" resultType="cn.bd.domain.Student">
select * from student where id=#{id}
</select>
<select id="getStudentByCity" resultType="cn.bd.domain.Student">
select * from student where city=#{city}
</select>
<select id="getStudentByName" resultType="cn.bd.domain.Student" >
select * from student where name like '%${name}%'
</select>
<select id="getStudentByIdName" resultType="cn.bd.domain.Student">
select * from student where id=#{id} and name like '%${name}%'
</select>
<insert id="addStudent" parameterType="cn.bd.domain.Student">
insert into student(id,name,city,age) values(null,#{name},#{city},#{age})
</insert>
<delete id="deleteStudent" parameterType="int">
delete from student where id=#{id}
</delete>
<update id="update" parameterType="cn.bd.domain.Student">
update student set name=#{name},city=#{city},age=#{age}
where id=#{id}
</update>
</mapper>
六.添加测试类
package cn.bd.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import cn.bd.dao.StudentDao;
import cn.bd.domain.Student;
import junit.framework.TestCase;
public class Test extends TestCase {
StudentDao studentDao;
SqlSession session;
{
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
session = sqlSessionFactory.openSession();
studentDao=session.getMapper(StudentDao.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getAllStudent(){
List<Student> list=studentDao.getAllStudent();
for (Student student : list) {
System.out.println(student);
}
}
public void getStudent(){
System.out.println(studentDao.getStudent(2));;
}
public void getStudentByName(){
List<Student> list=studentDao.getStudentByName("xiao");
for (Student student : list) {
System.out.println(student);
}
}
public void getStudentByIdName(){
Student list=studentDao.getStudentByIdName(3, "xiao");
System.out.println(list);
}
public void testadd() {
Student stu=new Student();
stu.setAge(5);
stu.setCity("aa");
stu.setName("xiaoming");
studentDao.addStudent(stu);
session.commit();
}
public void deleteStu() {
studentDao.deleteStudent(16);
session.commit();
}
public void testupdate() {
Student stu=new Student();
stu.setId(12);
stu.setAge(5);
stu.setCity("aa");
stu.setName("xiaomin");
studentDao.update(stu);
session.commit();
}
public void getStudentByCity(){
List<Student> list=studentDao.getStudentByCity("北京");
for (Student student : list) {
System.out.println(student);
}
}
}