现在的位置: 首页 > 综合 > 正文

mybaits入门(CURD)

2014年04月01日 ⁄ 综合 ⁄ 共 3517字 ⁄ 字号 评论关闭

1、首先建立工程导入mysql驱动jar包机mybatis-3.1.1.jar

2、编辑配置文件config.properties,mybatis-config.xml,StudentMapper.xml

config.properties

[java] view
plain
copy

  1. driver=com.mysql.jdbc.Driver  
  2. url=jdbc:mysql://localhost:3306/student  
  3. user=root  
  4. password=123  


mybatis-config.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>  
    <properties resource="config.properties"><!--读取属性文件-->  
    </properties>
    <typeAliases>
          <typeAlias alias="Student" type="com.mybaits.bean.Student"/>
   </typeAliases>  
    <environments default="development">  
        <environment id="development">  
            <transactionManager type="JDBC"/>  
            <dataSource type="POOLED">  
                <property name="driver" value="${driver}"/>  
                <property name="url" value="${url}"/>  
                <property name="username" value="${user}"/>  
                <property name="password" value="${password}"/>  
            </dataSource>       
        </environment>  
    </environments>  
    <mappers>  
        <mapper resource="com/mybatis/bean/StudentMapper.xml"/>  
    </mappers>  
</configuration>  


StudentMapper.xml(映射文件)

[html] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper   
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="com.mbatis.bean">  
  6.   
  7.  <select id="SelectAllStudent" resultType="Student">  
  8.     Select * from student<!-- 此处的student是数据库的student表 -->  
  9.  </select>  
  10.    
  11.  <select id="SelectOneStudent" resultType="Student" parameterType="int">  
  12.     select * from student where student.id=#{id};  
  13.  </select>  
  14.    
  15.  <insert id="AddStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">  
  16.     insert into Student(id,name,age,mail)   
  17.     values(#{id},#{name},#{age},#{mail});  
  18.  </insert>  
  19.    
  20.  <delete id="DeleteStudent" parameterType="int">  
  21.     delete from student where student.id=#{id}  
  22.  </delete>  
  23.    
  24.  <update id="UpdateStudent" parameterType="Student">  
  25.     update student set student.name=#{name},student.age=#{age},student.mail=#{mail}  
  26.     where student.id=#{id}  
  27.  </update>  
  28.    
  29. </mapper>    


StudentDaoImpl.java

[java] view
plain
copy

  1.   

[java] view
plain
copy

  1. package com.Dao.Impl;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.util.List;  
  5.   
  6.   
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  11.   
  12.   
  13. import com.Dao.*;  
  14. import com.Ibatis.Student;  
  15.    
  16.   
  17.   
  18. public class StudentDaoImpl implements StudentDao {  
  19.      private static SqlSessionFactory ssf ;  
  20.       static{  
  21.           String resource ="mybatis-config.xml";  
  22.           InputStream stream;  
  23.         try {  
  24.             stream = Resources.getResourceAsStream(resource);  
  25.              ssf= new SqlSessionFactoryBuilder().build(stream);  
  26.              stream.close();  
  27.         } catch (IOException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.       }  
  31.        
  32.     public List<Student> GetAllStduent() {  
  33.         List<Student> list = null;  
  34.         SqlSession session = null;  
  35.          try {  
  36.              session = ssf.openSession();  
  37.              list= session.selectList("com.mybatis.SelectAllStudent");  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         finally{  
  42.             session.close();  
  43.         }  
  44.          return list;  
  45.     }  
  46.   
  47.   
  48.     public void AddStudent(Student student) {  
  49.          SqlSession session = null;  
  50.          try {  
  51.              session = ssf.openSession();  
  52.             session.insert("com.mybatis.AddStudent",student);  

抱歉!评论已关闭.