MyBatis-新建项目: https://www.cnblogs.com/chenliugou/p/14502135.html
实现:主要是实现了查询单条数据、删除数据、修改、更新、查询全部数据的功能。
问题:在查询全部当中只能用:List students=session.selectList(statement);,如果用老师写的:List<Student> students=session.selectList(statement); 会报错。唉,还好知道用其他方法。
注:conf1没有作用的,只是笔记
- 整体情况
- studentMapper.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"> <!--namespace:该mapper.xml映射文件的唯一标识 --> <mapper namespace="org.lanqiao.entity.studentMapper"> <!--parameterType:输入参数的类型 resultType查询返回结果的类型,返回类型 --> <select id="queryStudentById" resultType="org.lanqiao.entity.Student" parameterType="int"> select * from student where stuno =#{stuno} </select> <insert id="addStudent" parameterType="org.lanqiao.entity.Student"> insert into student(stuno,stuname,stuage,graname) values(#{stuNo},#{stuName},#{stuAge},#{graName}) </insert> <delete id="deleteStudentByStuno" parameterType="int" > delete from student where stuno =#{stuno} </delete> <update id="updateStudentByStuno" parameterType="org.lanqiao.entity.Student"> update student set stuname=#{stuName},stuage=#{stuAge},graname=#{graName} where stuno=#{stuNo} </update> <select id="querAllStudents" resultType="org.lanqiao.entity.Student"> select * from student </select> </mapper>
- 测试Test.class
package org.lanqiao.entity; import java.io.IOException; import java.io.Reader; 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; public class Test { public static void queryStudentById() throws IOException{ //加载MyBatis文件(为了访问数据库) Reader reader=Resources.getResourceAsReader("conf.xml"); // SqlsessionFactory - connection SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //session - connection SqlSession session=sessionFactory.openSession(); String statement="org.lanqiao.entity.studentMapper.queryStudentById"; Student student= (Student) session.selectOne(statement,Integer.valueOf(1)); System.out.println(student); session.close(); } public static void queryAllStudent() throws IOException{ //加载MyBatis文件(为了访问数据库) Reader reader=Resources.getResourceAsReader("conf.xml"); // SqlsessionFactory - connection SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //session - connection SqlSession session=sessionFactory.openSession(); String statement="org.lanqiao.entity.studentMapper.querAllStudents"; List students=session.selectList(statement); System.out.println(students); session.close(); } //增加id public static void addStudent() throws IOException{ //加载MyBatis文件(为了访问数据库) Reader reader=Resources.getResourceAsReader("conf.xml"); // SqlsessionFactory - connection SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //session - connection SqlSession session=sessionFactory.openSession(); String statement="org.lanqiao.entity.studentMapper.addStudent";//执行的sql Student student=new Student(3,"22",21,"g1"); int count =session.insert(statement,student); session.commit(); System.out.println("增加"+count+"个学生"); session.close(); } //删除id public static void deleteStudentByStuno() throws IOException{ //加载MyBatis文件(为了访问数据库) Reader reader=Resources.getResourceAsReader("conf.xml"); // SqlsessionFactory - connection SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //session - connection SqlSession session=sessionFactory.openSession(); String statement="org.lanqiao.entity.studentMapper.deleteStudentByStuno";//执行的sql int count=session.delete(statement, Integer.valueOf(3)); session.commit(); System.out.println("删除"+count+"个学生"); session.close(); } //修改id public static void updateStudentByStuno() throws IOException{ //加载MyBatis文件(为了访问数据库) Reader reader=Resources.getResourceAsReader("conf.xml"); // SqlsessionFactory - connection SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader); //session - connection SqlSession session=sessionFactory.openSession(); String statement="org.lanqiao.entity.studentMapper.updateStudentByStuno";//执行的sql //修改那个人 Student student=new Student(); student.setStuNo(2); student.setStuName("lxs"); student.setStuAge(19); student.setGraName("s2"); int count =session.update(statement, student); session.commit(); System.out.println("修改"+count+"个学生"); session.close(); } public static void main(String[] arg) throws IOException{ //查询全部 queryAllStudent(); //更新 updateStudentByStuno(); //删除 deleteStudentByStuno(); //新增 addStudent(); //查询全部 queryAllStudent(); } }
- Student.java
package org.lanqiao.entity; public class Student { private int stuNo; private String stuName; private int stuAge; private String graName; public int getStuNo() { return stuNo; } public void setStuNo(int stuNo) { this.stuNo = stuNo; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getGraName() { return graName; } public void setGraName(String graName) { this.graName = graName; } public Student(int stuNo, String stuName, int stuAge, String graName) { super(); this.stuNo = stuNo; this.stuName = stuName; this.stuAge = stuAge; this.graName = graName; } public Student() { } public String toString() { return "Student [stuNo=" + this.stuNo + ", stuName=" + this.stuName + ", stuAge=" + this.stuAge + ", graName=" + this.graName + "]"; } }