Mybatis_mysql入门案例--增删改查

一、先建一个数据库test,再建一张user表,表里有两个字段:id 是自增长,name。

二、新建一个java项目,导入两个jar包:mysql-connector-java-5.1.17-bin.jar  和  mybatis-3.0.4.jar。

三、在src下建一个包:com.cb

四、在com.cb包下面有四个文件,可以直接复制粘贴进去:

       1、   User.java

                package com.cb;
                public class User {
                      private int id;
                      private String name;
                      public int getId() {
                                  return id;
                     }
                      public void setId(int id) {
                                 this.id = id;
                      }
                     public String getName() {
                                  return name;
                      }
                     public void setName(String name) {
                                   this.name = name;
                      }
                     public User(){
  
                       }
             }


       2、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>
                               <typeAlias type="com.cb.User"/>
                      </typeAliases>
                      <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=""/>
                      </dataSource>
                     </environment>
                    </environments>
                 <mappers>
                     <mapper resource="com/cb/User.xml"/>
                </mappers>
             </configuration>


       3、User.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.cb.User">  
                <!-- 增加一条记录 -->
               <insert id="addOneUser" parameterType="User" useGeneratedKeys="true" keyProperty="id">
                       insert into user(name) 
                       values(#{name}) 
             </insert>
             <!-- 删除一条记录 -->
           <delete id="deleteOneUser" parameterType="User">  
                   delete from user where id=#{id}      
          </delete>
         <!-- 更新一条记录 -->
         <update id="updateOneUser" parameterType="User"> 
                    update user set name=#{name} where id=#{id}  
         </update>  
      <!-- 查询一条记录 -->
      <select id="selectOneUser" resultType="User" parameterType="int">
           select * from user where id = #{id}
     </select>
    <!-- 查询全部记录 -->
        <select id="selectAllUser" resultType="User" parameterType="int">
              select * from user
     </select>    
   </mapper>


   4、  Test.java

package com.cb;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class Test {

 @SuppressWarnings("unchecked")
 public static void main(String[] args) throws IOException{
  String resource = "com/cb/configuration.xml";     
  Reader reader = Resources.getResourceAsReader(resource);     
  SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);     
  SqlSession session = sessionFactory.openSession();     
  try {       
   //增加一条新的记录
   User user=new User();
   user.setName("wangwu");
   session.insert("com.cb.User.addOneUser",user);
   session.commit();
   
   //删除一条记录
   /*session.delete("com.cb.User.deleteOneUser",11);
   session.commit();*/
   
   //更新一条数据
   /*User user=new User();
   user.setName("lisi");
   user.setId(2);
   session.update("com.cb.User.updateOneUser",user);
   session.commit();*/
   
   //按条件查询一条数据
   /*User user = (User) session.selectOne("com.cb.User.selectOneUser",8);
   System.out.println("id\tname");
   System.out.println(user.getId()+"\t"+user.getName());*/
      
   //查询表的全部数据
   ArrayList<User> list = (ArrayList<User>) session.selectList("com.cb.User.selectAllUser");
   for(int i=0;i<list.size();i++){
    System.out.println(list.get(i).getId()+"\t"+list.get(i).getName());
   }   
  } catch (Exception e) {   
   // TODO Auto-generated catch block   
   e.printStackTrace();  
  }finally{  
   session.close();  
  } 
 }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值