<?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.mybatis.inter.IStudentOperation"> <!-- 自定义resultMap --> <resultMap type="Student" id="resultListStudent"> <id column="id" property="id"/> <result column="name" property="name"/> <result column="password" property="password"/> </resultMap> <select id="getStudentById" parameterType="int" resultType="Student"> select * from student where id=#{id} </select> <select id="getAllStudents" resultMap="resultListStudent"> select * from student </select> <insert id="addStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id"> insert into student(name,password) values(#{name},#{password}) </insert> <update id="updateStudent" parameterType="Student"> update student set name=#{name}, password=#{password} where id=#{id} </update> <delete id="deleteStudent" parameterType="int"> delete from student where id=#{id} </delete> </mapper>
<?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> <!-- 命名别名 --> <typeAliases> <typeAlias alias="Student" type="com.mybatis.bean.Student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/mybatis/bean/Student.xml" /> </mappers> </configuration>
package com.mybatis.bean; public class Student { private int id; private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.mybatis.util; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessionFactory() throws IOException { if (sqlSessionFactory == null) { String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); } return sqlSessionFactory; } }
package com.mybatis.inter; import java.util.List; import com.mybatis.bean.Student; public interface IStudentOperation { public Student getStudentById(int id); public List<Student> getAllStudents(); public void addStudent(Student student); public void updateStudent(Student student); public void deleteStudent(int id); }
package com.mybatis.test; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.mybatis.bean.Student; import com.mybatis.inter.IStudentOperation; import com.mybatis.util.MybatisUtil; public class Test { public static void main(String[] args) { SqlSessionFactory factory=null; SqlSession session=null; try { factory=MybatisUtil.getSqlSessionFactory(); session=factory.openSession(); IStudentOperation studentOperation=session.getMapper(IStudentOperation.class); /** * 单条记录查询 */ // Student student = studentOperation.getStudentById(1); // System.out.println("student name :"+student.getName()); // System.out.println("student password :"+student.getPassword()); /** * 多条记录查询,重写resultMap */ // List<Student> allStudents = studentOperation.getAllStudents(); // if(allStudents != null && allStudents.size() >0) { // for(Student student:allStudents) { // System.out.println("student name :"+student.getName()); // System.out.println("student password :"+student.getPassword()); // } // } /** * 增加数据,一定要提交 */ // Student student=new Student(); // student.setName("ddd"); // student.setPassword("456"); // studentOperation.addStudent(student); // session.commit();//必须要提交事务,否则将不会记录到数据库中 // System.err.println(student.getId()); /** * 更新数据,一定要提交 */ // Student student = studentOperation.getStudentById(1); // student.setName("zcs"); // student.setPassword("111"); // studentOperation.updateStudent(student); // session.commit(); /** * 删除数据,一定要提交 */ // studentOperation.deleteStudent(1); // session.commit(); } catch (IOException e) { e.printStackTrace(); } } }