mybatis使用mapper代理进行各种数据库操作

本文使用mybatis对一个简单的数据表实现各种各样的增删改查工作,对mybatis中Mapper.xml配置详解

进行实操,步骤简单,本人亲测全部通过

注:我的项目目录:

 

 

下面开始吧

 

1.建立数据库表

 

 

建立数据库表SQL语句:

/*
Navicat MySQL Data Transfer

Source Server         : localhost
Source Server Version : 50621
Source Host           : localhost:3306
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50621
File Encoding         : 65001

Date: 2020-03-13 10:54:31
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for t_account
-- ----------------------------
DROP TABLE IF EXISTS `t_account`;
CREATE TABLE `t_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(11) DEFAULT NULL,
  `password` varchar(11) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_account
-- ----------------------------
INSERT INTO `t_account` VALUES ('1', '赵大波', 'pupupu', '48');
INSERT INTO `t_account` VALUES ('2', '孙俪', 'alloha~', '37');
INSERT INTO `t_account` VALUES ('4', '赵大波', 'pupupu', '48');
INSERT INTO `t_account` VALUES ('5', '赵大波', 'pupupu', '48');
INSERT INTO `t_account` VALUES ('7', '李阳', 'helloWorld', '36');

2.新建项目,配置maven:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.nenu</groupId>
    <artifactId>mybatisDemo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
    </dependencies>

    <!--支持把xml配置文件放在resources以外的地方,不加这些AccountMapper.xml读取不到-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

 

3.新建实体类Account

package com.nenu.entity;

public class Account {
    private int id;
    private String username;
    private String password;
    private int age;

    public Account() {
    }

    public Account(int id, String username, String password, int age) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.age = 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 String getPassword() {
        return password;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                '}';
    }
}

 

4.在mybatisConfig.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>
    <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/test?serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <mapper resource="com\nenu\respository\AccountMapper.xml"/>
    </mappers>
</configuration>

 

5.配置AccountMapper映射

(1)配置映射接口: AccountMapper

package com.nenu.respository;

import com.nenu.entity.Account;

import java.util.List;

public interface AccountMapper {
    //增加
    public int save(Account account);

    //更改
    public int update(Account account);

    //删除
    public int deleteById(int id);

    //查询所有
    public List<Account> findAll();

    //通过某或某几个条件查询
    public Account findById(int id);
    public List<Account> findByName(String name);
    public Account findById2(Integer id);
    public Account findById3(Long id);
    public List<Account> findByNameAndAge(String username, int age);

    //计数
    public int count();
    public Integer count2();

    //通过id找username
    public String findNameById(int id);
}

(2)配置映射对应的xml文件:AccountMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--写对应的接口名-->
<mapper namespace="com.nenu.respository.AccountMapper">
    <!--public int save(Account account);-->
    <insert id="save" parameterType="com.nenu.entity.Account">
        insert into t_account(username, password, age)values(#{username}, #{password}, #{age})
    </insert>

    <!--public int update(Account account);-->
    <update id="update" parameterType="com.nenu.entity.Account">
        update t_account set username = #{username}, password = #{password}, age = #{age} where id = #{id}
    </update>

    <!--public int deleteById(int id);-->
    <delete id="deleteById" parameterType="int">
        delete from t_account where id = #{id}
    </delete>

    <!--public List<Account> findAll();-->
    <select id="findAll" resultType="com.nenu.entity.Account">
        select * from t_account
    </select>

    <!--public Account findById(int id);-->
    <select id="findById" resultType="com.nenu.entity.Account">
        select * from t_account where id = #{id}
    </select>

    <!--public Account findByName(String name);-->
    <select id="findByName" parameterType="java.lang.String" resultType="com.nenu.entity.Account">
        select * from t_account where username = #{username}
    </select>

    <!--public Account findById2(Integer id);-->
    <select id="findById2" parameterType="java.lang.Integer" resultType="com.nenu.entity.Account">
        select * from t_account where id = #{id}
    </select>

    <!--public Account findById3(Long id);-->
    <select id="findById3" parameterType="java.lang.Long" resultType="com.nenu.entity.Account">
        select * from t_account where id = #{id}
    </select>

    <!--public Account findByNameAndAge(String username, int age);-->
    <select id="findByNameAndAge" resultType="com.nenu.entity.Account">
        select * from t_account where username = #{param1} and age = #{param2}
    </select>

    <!--public int count();-->
    <select id="count" resultType="int">
        select count(id) from t_account
    </select>

    <!--public Integer count2();-->
    <select id="count2" resultType="java.lang.Integer">
        select count(id) from t_account
    </select>

    <!--public String findNameById();-->
    <select id="findNameById" resultType="java.lang.String">
        select username from t_account where id = #{id}
    </select>
</mapper>

 

6.进行测试

<?xml version="1.0" encoding="UTF-8"?>
<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>com.nenu</groupId>
    <artifactId>mybatisDemo1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
    </dependencies>

    <!--支持把xml配置文件放在resources以外的地方,不加这些AccountMapper.xml读取不到-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

</project>

对应各种操作Mapper代码在mybatis中Mapper.xml配置详解 中有详细介绍,本文为其对应实操。

mybatis使用mapper代理进行各种数据库操作~小伙伴们加油!

 

posted @ 2020-03-13 11:12  山川湖海*  阅读(604)  评论(0编辑  收藏  举报