mybatis用xml文件完成增删改查

作为持久层框架,mybaits作用和hibernate一样,无非就是对数据库进行增删改查操作,下面我们通过4个实例感受一下。其实也就是增删改查,掌握这些,基本上就掌握了mybatis的基本操作了,下面一个个模块来完成。 
先看下工程整体内容: 
这里写图片描述 
在这个工程下将完成增删改查代码演示。 
   
1 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">   

<configuration>
    <settings>   
        <!-- changes from the defaults for testing -->   
        <setting name="cacheEnabled" value="false" />   
        <setting name="useGeneratedKeys" value="true" />   
        <setting name="defaultExecutorType" value="REUSE" />   
    </settings>

    <typeAliases>   
       <typeAlias alias="TbUser" type="org.liyb.mybatis.model.TbUser"/>   
    </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/springstud"/>   
              <property name="username" value="mysql"/>   
              <property name="password" value="mysql"/>   
           </dataSource>   
       </environment>   
    </environments>   
    <mappers>   
        <mapper resource="org/liyb/mybatis/dao/TbUserMapper.xml" />   
    </mappers>   
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

  以上这些环境配置(标签中的内容在以后和spring整合后,都会交给Spring来管理,现在暂时交给 mybatis来管理)中,修改成自己数据库相对应的情况即可,标签用来配置映射文件的,这些映射文件是针对不同的pojo的,这个示例中只操作一个User对象,所以只有一个配置文件,在sqlmap目录下的TbUserMapper.xml,在下文中可以看到。最后来看一下整个环境的结构:

  1. 程序的编写

      创建pojo类: 
      

package org.liyb.mybatis.model;

public class TbUser {

    private String uname;
    private String name;
    private String phone;
    private String password;

    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }

    ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

插入用户数据

<?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="org.liyb.mybatis.dao.TbUserMapper" >
    <resultMap id="BaseResultMap" type="org.liyb.mybatis.model.TbUser" >
        <id column="uname" property="uname" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 20160523 增添用户-->
    <insert id="insertUser" parameterType="org.liyb.mybatis.model.TbUser" >
        insert into tb_user (uname, name, phone, password)
            values
        (#{uname,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
    </insert>

</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

根据用户姓名查询用户信息

<?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="org.liyb.mybatis.dao.TbUserMapper" >
    <resultMap id="BaseResultMap" type="org.liyb.mybatis.model.TbUser" >
        <id column="uname" property="uname" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 20160523 增添用户-->
    <insert id="insertUser" parameterType="org.liyb.mybatis.model.TbUser" >
        insert into tb_user (uname, name, phone, password)
            values
        (#{uname,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
    </insert>
    <!-- 根据name查询 -->
    <select id="selectByPrimaryKey" parameterType="String" resultType="org.liyb.mybatis.model.TbUser">
        select * from tb_user where uname = #{name}
    </select>

</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

修改用户信息

<?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="org.liyb.mybatis.dao.TbUserMapper" >
    <resultMap id="BaseResultMap" type="org.liyb.mybatis.model.TbUser" >
        <id column="id" property="uname" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 20160523 增添用户-->
    <insert id="insertUser" parameterType="TbUser" >
        insert into tb_user (id, name, phone, password)
            values
        (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
    </insert>

    <!-- 根据name查询 -->
    <select id="selectByUserName" parameterType="String" resultType="org.liyb.mybatis.model.TbUser">
        select * from tb_user where id = #{name}
    </select>

    <!-- 修改用户名称 -->
    <update id="updateByUserName" parameterType="TbUser">
        update tb_user set name=#{name},phone=#{phone},password=#{password} where id=#{id}
    </update>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

删除用户:

<?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="org.liyb.mybatis.dao.TbUserMapper" >
    <resultMap id="BaseResultMap" type="org.liyb.mybatis.model.TbUser" >
        <id column="id" property="uname" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="phone" property="phone" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
    </resultMap>

    <!-- 20160523 增添用户-->
    <insert id="insertUser" parameterType="TbUser" >
        insert into tb_user (id, name, phone, password)
            values
        (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
    </insert>

    <!-- 根据name查询 -->
    <select id="selectByUserName" parameterType="String" resultType="org.liyb.mybatis.model.TbUser">
        select * from tb_user where id = #{name}
    </select>

    <!-- 修改用户名称 -->
    <update id="updateByUserName" parameterType="TbUser">
        update tb_user set name=#{name},phone=#{phone},password=#{password} where id=#{id}
    </update>

        <!-- 根据用户id删除用户信息 -->
    <update id="deleteUserById" parameterType="String">
        delete from tb_user where id=#{id}
    </update>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

至此增删改产全部实现,下面贴出全部代码。

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>
    <settings>   
        <!-- changes from the defaults for testing -->   
        <setting name="cacheEnabled" value="false" />   
        <setting name="useGeneratedKeys" value="true" />   
        <setting name="defaultExecutorType" value="REUSE" />   
    </settings>

    <typeAliases>   
       <typeAlias alias="TbUser" type="org.liyb.mybatis.model.TbUser"/>   
    </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/springstud"/>   
              <property name="username" value="mysql"/>   
              <property name="password" value="mysql"/>   
           </dataSource>   
       </environment>   
    </environments>   
    <mappers>   
        <mapper resource="org/liyb/mybatis/dao/TbUserMapper.xml" />   
    </mappers>   
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

TbUserMapper.java

package org.liyb.mybatis.dao;

import org.liyb.mybatis.model.TbUser;

public interface TbUserMapper {

    TbUser selectByUserName(String uname);

    int updateByUserName(TbUser user);

    public void insertUser(TbUser user);

    public TbUser getUser(String name);

    void deleteUserById(String id);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

TbUserMapper.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>
    <settings>   
        <!-- changes from the defaults for testing -->   
        <setting name="cacheEnabled" value="false" />   
        <setting name="useGeneratedKeys" value="true" />   
        <setting name="defaultExecutorType" value="REUSE" />   
    </settings>

    <typeAliases>   
       <typeAlias alias="TbUser" type="org.liyb.mybatis.model.TbUser"/>   
    </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/springstud"/>   
              <property name="username" value="mysql"/>   
              <property name="password" value="mysql"/>   
           </dataSource>   
       </environment>   
    </environments>   
    <mappers>   
        <mapper resource="org/liyb/mybatis/dao/TbUserMapper.xml" />   
    </mappers>   
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

TbUser.java

package org.liyb.mybatis.model;

public class TbUser {

    private String id;
    private String name;
    private String phone;
    private String password;

    public TbUser(){

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public TbUser(String id,String name,String phone,String password){
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.password = password;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

测试类:

package test.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.liyb.mybatis.dao.TbUserMapper;
import org.liyb.mybatis.model.TbUser;

public class TestMyBatis {

    public static SqlSessionFactory sqlSessionFactory = null;

    static{
        sqlSessionFactory = MyBatisUtil.getSqlsessionfactory();
    }
    /**
     * 
     *author: lee by 2016年6月20日
     *description:mybatis插入用户数据
     *@param 
     *@return void
     */
    public static void testAdd() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
            TbUser user = new TbUser("liybk", "liyb","18601387505","123");
            userMapper.insertUser(user);
            sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中
            } finally {
                sqlSession.close();
            }
    }

    /**
     * 
     *author: lee by 2016年6月20日
     *description:mybatis根据姓名查询用户
     *@param 
     *@return void
     */
    public static void testQuery(String name){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
            TbUser users = userMapper.selectByUserName(name);
            System.out.println("该用户的姓名是:"+users.getName()+" ,手机号码为:"+users.getPhone());
        }finally{
            sqlSession.close();
        }
    }


    public static void testUpdate(String id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
            TbUser user = userMapper.selectByUserName(id);
            user.setName("songzhihui3");
            int count = userMapper.updateByUserName(user);
            sqlSession.commit();
            System.out.println(count);
        }finally{
            sqlSession.close();
        }
    }

    public static void testDelete(String id){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try{
            TbUserMapper userMapper = sqlSession.getMapper(TbUserMapper.class);
            userMapper.deleteUserById(id);
            sqlSession.commit();
        }finally{
            sqlSession.close();
        }
    }
    public static void main(String[] args){
        //TestMyBatis.testAdd();
        //TestMyBatis.testQuery("liybk");
        //TestMyBatis.testUpdate("songzh2");
        TestMyBatis.testDelete("songzh2");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

基于maven工程的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.liyb.mybatis.stud</groupId>
  <artifactId>mybatisHelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.0</version>
    </dependency>

    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

        <!-- mybatis-generator -->
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
         </plugin>

    </plugins>
  </build>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值