Mybatis的环境搭建实现增删除改查和批量的增加,和多个id同时查询

相关的结构

Mybatis的环境搭建实现增删除改查和批量的增加,和多个id同时查询_第1张图片
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">
    <parent>
        <artifactId>TeacherSpringCompnentScanartifactId>
        <groupId>com.zjjgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>
    <artifactId>TeacherMybatisartifactId>


    <dependencies>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.45version>
        dependency>

        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>


        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.13.2version>
            <scope>testscope>
        dependency>





        <dependency>
            <groupId>org.apache.logging.log4jgroupId>
            <artifactId>log4j-coreartifactId>
            <version>2.8.2version>
        dependency>

    dependencies>

project>

配置相关的日志:
在mybatis-config.xml文件中加上:

<!--    日志的相关的信息的配置-->
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

在ReSource目录中创建log4j.properties文件加上如下的内容:

log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout

log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.Java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

UserMapper.xml文件:



<mapper namespace="com.zjj.dao.UserDao">

    

    <resultMap id="userResultMap" type="User">




        
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="isadmin" column="isadmin"/>

    resultMap>

    
    <select id="queryById" resultMap="userResultMap">
        select * from tb_user where id=#{id}
    select>

    
    <select id="queryAllUsers" resultType="User">
      select *from tb_user
    select>




    <select id="queryUsersBymouhu" parameterType="string" resultMap="userResultMap">

     select *from tb_user where username like "%${mohu}%"
    select>

 <select id="queryUsersBymouhu2" parameterType="string" resultMap="userResultMap">
     select *from tb_user where username like "%${mo}%" or password like "%${mo}%"
 select>







    
    <insert id="addUser" parameterType="User" >
        INSERT INTO tb_user ( username, password, isadmin) VALUES ( #{username},#{password} ,#{isadmin} )
    insert>
    <insert id="addUser2" parameterType="User" keyProperty="id" useGeneratedKeys="true">
        insert into tb_user(username,password,isadmin) values(#{username},#{password},#{isadmin})
    insert>

    
    <update id="updateUser" parameterType="User">
       UPDATE `tb_user` SET `username` = #{username}, `password` = #{password}, `isadmin` = #{isadmin} WHERE `id` = #{id};

    update>

    
 <delete id="deleteById" parameterType="long">
     delete from tb_user where id=#{id}

 delete>
    





    <select id="queryStudentsByUsernameAndAdmin" parameterType="User" resultMap="userResultMap">
        select *from  tb_user where username=#{username} and isadmin=#{isadmin}
    select>


   <select id="queryUserByids" parameterType="List" resultMap="userResultMap">
       select *from tb_user where id in

--        注意这里的list一定要小写
       <foreach collection="list" item="id" index="i" open="(" separator="," close=")">
           #{id}
       foreach>
   select>


    <insert id="insertBatch" parameterType="List">
        INSERT INTO tb_user ( username, password, isadmin) VALUES
        <foreach collection="list" item="item" index="index"   separator=",">
            (#{item.username}, #{item.password}, #{item.isadmin})
        foreach>
    insert>





mapper>

User文件:

package com.zjj.pojo;

public class User {
     
//    建议属性名和字段名保持一致,这个可以不用resultMap
    private  Long id;
    private  String username;
    private  String password;
    private  Integer isadmin;

    public User() {
     
    }

    public User(String username, String password, Integer isadmin) {
     
        this.username = username;
        this.password = password;
        this.isadmin = isadmin;
    }

    public User(Long id, String username, String password, Integer isadmin) {
     
        this.id = id;
        this.username = username;
        this.password = password;
        this.isadmin = isadmin;
    }

    public Long getId() {
     
        return id;
    }

    public void setId(Long 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 Integer getIsadmin() {
     
        return isadmin;
    }

    public void setIsadmin(Integer isadmin) {
     
        this.isadmin = isadmin;
    }

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

UserDao文件:

package com.zjj.dao;

import com.zjj.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface UserDao{
     
//测试一下看看是否有连接的错误
 public User queryById(Long id);
// 查询所有的User
 public List<User> queryAllUsers();
//   通过id删除数据
    public void deleteById(Long id);

// 添加一个User
    public void addUser(User user);

//    添加并返回添加后的自增主键
    public void addUser2(User user);
//    更新
    public void updateUser(User user);




//    模糊查询
    public List<User> queryUsersBymouhu(@Param("mohu") String mohu);

//    多个模糊查询
    public List<User> queryUsersBymouhu2(@Param("mo") String mo);

//  查询直接查询
    public List<User> queryUserByUsernameAndisadmin(User user);


//    根据id的集合查询所有
    public List<User> queryUserByids(List<Long> ids);

//    批量添加
   public void insertBatch(List<User> users);











}

你可能感兴趣的:(mysql,Mybatis)