你的位置:首页 > Java教程

[Java教程]Mybatis环境


第一步:下载jar包并导入

1、mysql驱动包

2、mybatis环境包

第二步:创建MYSQL数据库

由于这是用于测试,只创建了test-usreinfo数据表

第三步:在src文件夹中创建mybatis-cfg.

<?

 第四步:在src文件中撞见jar包

1、com.shengmu.model:数据模型包

2、com.shengmu.util:处理工具包

3、com.shengmu.DAO:数据库操作包

4、com.shengmu.test:测试包

第五步:在com.shengmu.model:数据模型包创建User.class

 1 package com.shengmu.model; 2  3 public class User { 4    5   private int id; 6   private String username; 7   private String userpaw; 8   public int getId() { 9     return id;10   }11   public void setId(int id) {12     this.id = id;13   }14   public String getUsername() {15     return username;16   }17   public void setUsername(String username) {18     this.username = username;19   }20   public String getUserpaw() {21     return userpaw;22   }23   public void setUserpaw(String userpaw) {24     this.userpaw = userpaw;25   }26   27   @Override28   public String toString() {29     return "User [id=" + id + ", username=" + username + ", userpaw=" + userpaw + "]";30   }31   32 33 }

第六步:创建usermapper.

<??><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.shengmu.model.usermapper" >  <select id="SelectUserByUsername" parameterType="int" resultType="com.shengmu.model.User" >    select * from userinfo where id = #{id}  </select></mappe

第七步:在mybatis-cfg.

 

<??><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>  <environments default="mybatis-mysql">    <environment id="mybatis-mysql">      <transactionManager type="jdbc"></transactionManager>      <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="" />      </dataSource>    </environment>  </environments>  <mappers>    <mapper resource="com/shengmu/model/usermapper. />  </mappers></configuration>

其中:<mappers> <mapper resource="com/shengmu/model/usermapper./> </mappers>是usermapper.

第八步:在com.shengmu.test中加入测试类TestSelect.class

package com.shengmu.test;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.shengmu.model.User;public class TestSelect {  public static void main(String[] args) {    String resource = "mybatis-cfg.;    try {      InputStream inputStream = TestSelect.class.getClassLoader().getResourceAsStream(resource);      SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);      SqlSession session = sessionFactory.openSession();      String statment = "com.shengmu.model.usermapper.SelectUserByUsername";      User user = session.selectOne(statment,1);            session.commit();      System.out.println(user.getUsername());      session.close();    } catch (Exception e) {      // TODO Auto-generated catch block      e.printStackTrace();    }  }}

最后run as --java application.