mybaits

Posted yuanning1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybaits相关的知识,希望对你有一定的参考价值。

   mybatis的基础安装使用

1.jar包  :相关jar包的导入,包含mysql-connector-java-3.1.12-bin.jar          mybatis-3.2.3.jar     commons-logging-1.1.1.jar  等。

    2.po类  :此处以学生类为例

    public class Student implements Serializable{
    private Integer id;
    private int age;
    private String name;
    public Student() {
    super();
    }
    public Student(int age, String name) {
    super();
    this.age = age;
    this.name = name;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    @Override
    public String toString() {
    return "Student [id=" + id + ", age=" + age + ", name=" + name + "]";
    }
    }

    3.dao接口

    public interface StudentDaoI {
    void insert(Student student);
    Student select (int id);
    }

    4.mapper文件-----》student.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">

    <mapper namespace="com.dao.StudentDaoI">
        <insert id="insert" parameterType="Student">
            insert into student(age,name) values(#{age},#{name})
        </insert>


        <select id="select" parameterType="int" resultType="Student">
             select * from student where id = #{id}
        </select>
    </mapper>

    5.mybatis配置文件

    <?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">

     //po类的别名
    <configuration>
         <typeAliases>
             <typeAlias type="com.po.Student" alias="Student" ></typeAlias>
         </typeAliases>
    //连接数据库参数     
         <environments default="development">
            <environment id="development">
               <transactionManager type="JDBC"></transactionManager>
               <dataSource type="POOLED">
                 <property name="driver" value="com.mysql.jdbc.Driver"/>
                 <property name="url" value="jdbc:mysql://localhost:3306/stub"/>
                 <property name="username" value="root"/>
                 <property name="password" value="123456"/>
               </dataSource>
            </environment>
         </environments>

          //mapper文件的位置
           <mappers>
                <mapper resource="student.xml"/>
           </mappers>
    </configuration>

    6.创建测试类StudentTest

    public class StudentTest {
    private SqlSession session;
    @Before
    public void before(){

    InputStream  in=null ;
    try {
    //解析xml
     in = Resources.getResourceAsStream("SqlmapConfiguration.xml");
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       //创建sf
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(in);
           //创建session
    session = sf.openSession();
    }


    @After
    public void after(){
    session.close();
    }

    @Test
    public void testInsert(){
    StudentDaoI sdao = session.getMapper(StudentDaoI.class);
    sdao.insert(new Student(18,"张二"));
    session.commit();
    }

    @Test
    public void testSelect(){
    StudentDaoI sdao = session.getMapper(StudentDaoI.class);
    Student s = sdao.select(2);
    System.out.println(s);
    }
    }

以上是关于mybaits的主要内容,如果未能解决你的问题,请参考以下文章