`
lichaobao
  • 浏览: 45388 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

mybatis快速入门--配置文件配置数据接口形式

阅读更多

创建表:

create table user
(
  id int(11) not null  auto_increment,
  username varchar(15) not null,
  age int(11) not null,
  primary key(id),
)engine=innodb default charset=utf8 auto_increment=1;

 工程图:

 

mybatis连接MySQL数据库配置文件 configuration.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> 
    <typeAliases> 
        <!--给实体类起一个别名 user --> 
        <typeAlias type="com.lcb.domain.User" alias="User" /> 
   </typeAliases> 
    <!--数据源配置  这块用 mysql数据库 --> 
    <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/mybatis" /> 
    <property name="username" value="root" /> 
    <property name="password" value="" /> 
            </dataSource> 
  </environment> 
  </environments> 
    <mappers> 
        <!--user.xml装载进来  同等于把“dao”的实现装载进来 -->      
        <mapper resource="com/lcb/mybatis/usermapper.xml" />  
    </mappers> 
</configuration>

 

创建实体类 User.java

public class User {
 private int id;
    private String username;
    private int age;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 } 
}

创建数据接口 UserDao.java

public interface UserDao {
 public int insert(User user);
 public int update(User user);
 public int delete(User user);
 public int countAll();
 public ArrayList<User> findAll();
 public ArrayList<User> findAllByIds(ArrayList<Integer> ids);
 public User findById(int id);
 public User findById1(int id);
}

 

 

配置数据接口配置文件 usermapper.xml  <id命名就是数据接口中的方法名>

<?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.lcb.dao.UserDao"> 
    <select id="insert" parameterType="User" resultType="int"> 
  insert into student(username,age)values(#{username},#{age})
 </select> 
  <select id="update" parameterType="User" resultType="int"> 
  update student set  username=#{username},age=#{age} where id=#{id}
 </select> 
  <select id="delete" parameterType="int" resultType="int"> 
  delete from student where id=#{id} 
 </select> 
  <select id="countAll" resultType="int"> 
  select count(*) from student
 </select> 
  <select id="findAll" resultType="User"> 
  select * from student
 </select> 
  <select id="findAllByIds" resultType="User"> 
  select * from student where id in

     <!--              参数是list集合-->
     <foreach collection="list" item="ids"  open="(" separator="," close=")"> 
         #{ids} 
      </foreach>   
 </select> 
  <select id="findById" parameterType="int" resultType="User"> 
  select * from student where id=#{id} 
 </select> 
</mapper>

 

测试   MyBatisTest.java

 

public class MyBatisTest {
  private static SqlSessionFactoryBuilder  sqlSessionFactoryBuilder = null;
  private static SqlSessionFactory sqlSessionFactory = null;
     private static Reader reader;

     static{
         try{
             reader  = Resources.getResourceAsReader("configuration.xml");
             sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
             sqlSessionFactory = sqlSessionFactoryBuilder.build(reader);
         }catch(Exception e){
             e.printStackTrace();
         }
     }

     public static SqlSessionFactory getSession(){
         return sqlSessionFactory;
     }
     public static void main(String[] args) {
         SqlSession session = sqlSessionFactory.openSession();
         try {
          //获取数据接口UserDao
             UserDao userDao = session.getMapper(UserDao.class);          

         User user1 = new User();
          user1.setUsername("aaa");
          user1.setAge(28);
             //执行UserDao的insert方法
          userDao.insert(user1);

             //执行UserDao的countAll方法
          System.out.println(userDao.countAll());  //输出1
             //执行UserDao的findById方法
          User user = (User) userDao.findById(1);
          System.out.println(user.getUsername());  //输出aaa
          ArrayList<Integer> list = new ArrayList<Integer>();
          list.add(1);
          list.add(2);
             //执行UserDao的findAllByIds方法
          System.out.println(userDao.findAllByIds(list).size());  //输出1
         }catch(Exception e){
          
         } finally {
          session.close();
         }
     }
}

 

  • 大小: 8.9 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics