前提:Mybatis环境搭建完成
直接上代码吧(我这里是配置文件方式来实现的):
一、配置文件方式:
数据库表格:
StudentDao.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.chenlei.dao.StudentDao">
<!--查询所有-->
<select id="findAll" resultType="com.chenlei.domain.Student">
select * from student;
</select>
<!--增加学生-->
<insert id="saveStudent" parameterType="com.chenlei.domain.Student">
insert into student(name,sex,birthday) values(#{name},#{sex},#{birthday});
</insert>
<!--修改学生-->
<update id="updateStudent" parameterType="com.chenlei.domain.Student">
update student set name = #{name},sex = #{sex},birthday=#{birthday} where id=#{id};
</update>
<!--删除学生-->
<delete id="deleteStudent" parameterType="Integer">
delete from student where id = #{sid};
</delete>
<!--根据id查询学生-->
<select id="findById" parameterType="int" resultType="com.chenlei.domain.Student">
select * from student where id = #{sid};
</select>
<!--根据名称进行模糊查询-->
<select id="findByName" parameterType="string" resultType="com.chenlei.domain.Student">
select * from student where name like #{name};
</select>
<!--查询学生总数-->
<select id="findTotal" resultType="int">
select count(*) from student;
</select>
</mapper>
测试类:
public class MybatisTest {
private InputStream in;
private SqlSession sqlSession;
private StudentDao studentDao;
//用于测试方法测试之前执行
@Before
public void init() throws IOException {
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.使用工厂生产SqlSession对象
sqlSession = factory.openSession();
///4.使用SqlSession创建Dao接口的代理对象
studentDao = sqlSession.getMapper(StudentDao.class);
}
//用于测试方法之后执行
@After
public void destroy() throws IOException {
//提交事务
sqlSession.commit();
//6.释放资源
sqlSession.close();
in.close();
}
@Test
public void findAll() {
//执行查询所有对象方法
List<Student> students = studentDao.findAll();
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testSave(){
Student student = new Student();
student.setName("mybatis");
student.setSex("女");
student.setBirthday(new Date());
//5.执行保存方法
studentDao.saveStudent(student);
}
@Test
public void testUpdate(){
Student student = new Student();
student.setName("coco");
student.setId(25);
student.setSex("女");
student.setBirthday(new Date());
//执行保存方法
studentDao.updateStudent(student);
}
@Test
public void testDelete(){
//执行删除方法
studentDao.deleteStudent(22);
}
@Test
public void testSelectOne(){
//执行查询一个方法
Student student = studentDao.findById(25);
System.out.println(student);
}
@Test
public void testfindByName(){
//执行根据名字模糊查询方法
List<Student> students = studentDao.findByName("%王%");
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testfindTotal(){
//执行函数统计查询方法
int total = studentDao.findTotal();
System.out.println(total);
}
}
StudentDao:
* 持久层接口
*/
public interface StudentDao {
/**
* 查找所有操作
* @return
*/
List<Student> findAll();
/**
* 保存学生
*/
void saveStudent(Student student);
/**
* 更新操作
* @param student
*/
void updateStudent(Student student);
/**
* 根据id删除学生
* @param id
*/
void deleteStudent(Integer id);
/**
* 根据id查询学生
* @param id
*/
Student findById (Integer id);
/**
* 根据名称进行模糊查询
* @return
* @param s
*/
List<Student> findByName(String s);
/**
* 查询总学生数
* @return
*/
int findTotal();
}
二、注解方式:
注意:注解方式与配置文件方式除了需要修改的部分有代码展示,其他与上面配置一致;且配置文件代码与注解方式代码不能同 时存在,所以需要把studentDao.xml删除。
StudentDao修改:
* 持久层接口
*/
public interface StudentDao {
/**
* 查找所有操作
* @return
*/
@Select("select * from student")
List<Student> findAll();
/**
* 保存学生
*/
@Insert("insert into student(name,sex,birthday) values(#{name},#{sex},#{birthday})")
void saveStudent(Student student);
/**
* 更新操作
* @param student
*/
@Update("update student set name = #{name},sex = #{sex},birthday=#{birthday} where id=#{id}")
void updateStudent(Student student);
/**
* 根据id删除学生
* @param id
*/
@Delete("delete from student where id = #{sid}")
void deleteStudent(Integer id);
/**
* 根据id查询学生
* @param id
*/
@Select("select * from student where id = #{sid};")
Student findById (Integer id);
/**
* 根据名称进行模糊查询
* @return
* @param s
*/
@Select("select * from student where name like #{name}")
List<Student> findByName(String s);
/**
* 查询总学生数
* @return
*/
@Select("select count(*) from student")
int findTotal();
}
SqlMapConfig.xml(改为注解模式):
<!--指定映射配置文件位置;映射配置文件指的是每个dao独立的配置文件-->
<!-- <mappers>
<mapper resource="com/chenlei/dao/StudentDao.xml"/>
</mappers>-->
<!--如果是注解,则应该使用class属性-->
<mappers>
<mapper class="com.chenlei.dao.StudentDao"/>
</mappers>
对于细节配置不清楚的可以查看我的入门总结: