目录

- public class Student {
- private Integer id;
- private String name;
- private Integer age;
-
- public Student() {
- }
-
- public Student(Integer id, String name, Integer age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }

- //查询全部
- @Select("SELECT * FROM student")
- public abstract List
selectAll();

<package name="demo5.mapper"/> 
- @Test
- public void selectAll() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- List
list = mapper.selectAll(); - //6.处理结果
- for (Student student : list) {
- System.out.println(student);
- }
- //7.释放资源
- sqlSession.close();
- is.close();
- }


- //新增操作
- @Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
- public abstract Integer insert(Student stu);

- @Test
- public void insert() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- Student stu = new Student(5,"倪七",26);
- Integer result = mapper.insert(stu);
- //6.处理结果
- System.out.println(result);
- //7.释放资源
- sqlSession.close();
- is.close();
- }




- //修改操作
- @Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
- public abstract Integer update(Student stu);

- @Test
- public void update() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- Student stu = new Student(5,"倪九",99);
- Integer result = mapper.update(stu);
- //6.处理结果
- System.out.println(result);
- //7.释放资源
- sqlSession.close();
- is.close();
- }



- //删除操作
- @Delete("DELETE FROM student WHERE id=#{id}")
- public abstract Integer delete(Integer id);

- @Test
- public void delete() throws Exception{
- //1.加载核心配置文件
- InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
- //2.获取SqlSession工厂对象
- SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
- //3.通过工厂对象获取SqlSession
- SqlSession sqlSession = sqlSessionFactory.openSession(true);
- //4.获取StudentMapper接口的实现类对象
- StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
- //5.调用实现类的方法,接收结果
- Integer result = mapper.delete(5);
- //6.处理结果
- System.out.println(result);
- //7.释放资源
- sqlSession.close();
- is.close();
- }

