`

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();
	}
}

 

 

分享到:
评论

相关推荐

    ibatis 学习笔记

    ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记 ibatis 学习笔记

    IBatis学习笔记以及使用心得

    IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得IBatis学习笔记以及使用心得

    ibatis学习笔记

    ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记 ibatis学习笔记

    iBatis2学习笔记

    6.iBatis2学习笔记:一对多映射(双向).doc 7.iBatis2学习笔记:多对多映射(双向) .doc 8.iBatis2学习笔记:总结与思考.doc 9.iBatis2实体状态图解.doc 10.iBatis insert操作陷阱.doc 每章都有小例子。 呵呵,希望...

    ibatis学习笔记.txt

    ibatis学习笔记.txtibatis学习笔记.txtibatis学习笔记.txt

    持久层框架ibatis学习笔记

    这篇iBatis 学习笔记是跟着传智播客的视频学习整理的,理解上难免有些错误,请以视频为 根本,有些地方笔记中没有整理到,因为这是我之后看着自己做的工程项目总结的,和视频 不完全一致。请谅解。

    iBATIS学习笔记

    iBATIS学习笔记 使用 iBATIS 开发近一年了,都是在 Google 中现学现用,是时候为自己总结看看这一年都收获了些什么。无奈的是英文水平实在是太差了,官方文档看起来太吃力,所以到图书馆借了这本《iBATIS 实战》这是 ...

    iBatis学习笔记

    iBatis学习笔记,有利于初步了解iBatis。

    Ibatis学习笔记,文档,资源6合1

    Ibatis学习笔记,文档,资源,6合1,有兴趣自学ibatis的童鞋的最佳选择。

    Ibatis的学习笔记

    Ibatis的学习笔记,说明Ibatis的使用

    ibatis学习笔记(一)

    NULL 博文链接:https://microjava.iteye.com/blog/536986

    iBATIS学习笔记(一)查询记录

    e) 在项目中新建实体类Test.java,内容如下: package com.entity; public class StuInfo { private Integer sid; private String stuName; public Integer getSid() { ... public void setSid(Integer sid) { ...

    ibatis教程学习笔记

    学习ibatis的基础资料~~~ 要好好的利用学习~~~学习别人的笔记是你进步的动力~~

Global site tag (gtag.js) - Google Analytics