ibaties学习笔记(一)

// MyBaties是持久层框架,搭建时需要导入相应的jar包mybatis-3.3.0.jar

// 1.编写相应的配置文件conf.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>
   <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://localhost:3306/test" />
         <property name="username" value="root" />
         <property name="password" value="123456" />
       </dataSource>
     </environment>
   </environments>
    <mappers>
    <mapper resource="com/cc/TestMyBaties/mapping/Student.xml"/>
  </mappers>
 </configuration>

 // 2.编写实体类

 

public class Student {
	private int sid;
	private String sname;
	private String sex;
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String sname, String sex) {
		super();
		this.sname = sname;
		this.sex = sex;
	}

	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}

 

 

// 3.实体类的映射文件(该映射文件主要是用来操作数据,如SQL语句)

<?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="Student">
	<!-- 所返回的例表 -->
	<resultMap type="com.cc.TestMyBaties.entity.Student" id="resultListStus">
		<id column="sid" property="sid" />
		<id column="sname" property="sname" />
		<id column="sex" property="sex" />
	</resultMap>
	
	<select id="selectStudentById" parameterType="int" resultType="com.cc.TestMyBaties.entity.Student">
		select * from Student where sid = #{id}
	</select>
	<select id="selectStudents" parameterType="int" resultMap="resultListStus">
		select * from Student
	</select>
	
	<insert id="addStudent" parameterType="com.cc.TestMyBaties.entity.Student" useGeneratedKeys="true" keyProperty="sid">
		insert into Student(sname,sex) values(#{sname},#{sex})
	</insert>
	
	<update id="editStudent" parameterType="com.cc.TestMyBaties.entity.Student">
		update Student set sname=#{sname},sex=#{sex} where sid=#{sid}
	</update>
	
	<delete id="delStudent" parameterType="int">
		delete from Student where sid=#{sid}
	</delete>
</mapper>

 

 

// 4.编写连接类

public class MyBatiesUtil {
	private static final SqlSessionFactory sessionFactory;

	static {
		 //mybatis的配置文件
	    String resource = "conf.xml";
	    //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
	    InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
	    //构建sqlSession的工厂
	    sessionFactory = new SqlSessionFactoryBuilder().build(is);
	    //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
	    //Reader reader = Resources.getResourceAsReader(resource); 
	    //构建sqlSession的工厂
	}
	
	public static SqlSessionFactory getSessionfactory() {
		return sessionFactory;
	}
}

 // 5.编写测试类

 

public class Test extends MyBatiesUtil{

	/**
	 * 根据ID查询
	 */
	public void findById(){
		SqlSession session = this.getSessionfactory().openSession();
		String statement = "Student.selectStudentById";//映射sql的标识字符串
	    //执行查询返回一个唯一user对象的sql
	    Student student = session.selectOne(statement, 1);
	    System.out.println("name:" + student.getSname() + "   " + "sex:" + student.getSex());
	    session.close();
	}
	
	public void findAll(){
		SqlSession session = this.getSessionfactory().openSession();
		String statement = "Student.selectStudents";//映射sql的标识字符串
	    //执行查询返回一个唯一user对象的sql
	    List<Student> students = session.selectList(statement);
	    for(Student stu : students){
	    	System.out.println("name:" + stu.getSname() + "   " + "sex:" + stu.getSex());
	    }
	    
	    session.close();
	}
	
	public void addStudent() {
		Student stu = new Student();
		stu.setSname("xiaohong");
		stu.setSex("woman");
		
		SqlSession session = this.getSessionfactory().openSession();
		String statement = "Student.addStudent";//映射sql的标识字符串
		session.insert(statement,stu);
		session.commit();
		session.close();
	}
	
	public void editStudent() {
		Student stu = new Student();
		stu.setSid(5);
		stu.setSname("xiaohong");
		stu.setSex("gir");
		
		SqlSession session = this.getSessionfactory().openSession();
		String statement = "Student.editStudent";//映射sql的标识字符串
		session.update(statement, stu);
		session.commit();
		session.close();
	}
	
	
	public void delStudent(){
		SqlSession session = this.getSessionfactory().openSession();
		String statement = "Student.delStudent";//映射sql的标识字符串
		session.delete(statement, 1);
		session.commit();
		session.close();
	}
	
	public static void main(String[] args) {
		Test test = new Test();
		test.delStudent();
	}
}

 

 

你可能感兴趣的:(ibaties学习笔记(一))