mybatis对mysql数据库的增删改查操作

  1. 新建项目mybatis
    mybatis对mysql数据库的增删改查操作_第1张图片
    mybatis对mysql数据库的增删改查操作_第2张图片

  2. 在web文件夹下新建lib包,并导入相应jar包,并选中lib包右键选择“Add As Library”
    mybatis对mysql数据库的增删改查操作_第3张图片

  3. src文件下建立相应项目文件夹,并将“sqlMapConfig.xml”复制到com文件夹下 mybatis对mysql数据库的增删改查操作_第4张图片

  4. 项目总览
    mybatis对mysql数据库的增删改查操作_第5张图片

  5. User.java

package com.zhongruan.bean;

public class User {
    private int id;
    private String username;
    private String password;

    public User(){}
    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    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;
    }

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

  1. IUserDao.java
package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserDao {
    List<User> findAll();
    void deleteById(int id);
    void updateById(User user);
    void insert(User user);

}

  1. UserMapper1.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.zhongruan.dao.IUserDao">
    <select id="findAll" resultType="com.zhongruan.bean.User">
        select * from tb_user
    </select>

    <select id="deleteById" parameterType="int">
        delete from tb_user where id=#{id}
    </select>

    <select id="updateById" parameterType="com.zhongruan.bean.User">
        update tb_user set username=#{username},password=#{password} where id=#{id}
    </select>

    <select id="insert" parameterType="com.zhongruan.bean.User">
        insert into tb_user set id=#{id},username=#{username},password=#{password}
    </select>


</mapper>
  1. Test.java
package com.zhongruan.test;


        import com.zhongruan.bean.User;
        import org.apache.ibatis.io.Resources;
        import org.apache.ibatis.session.SqlSession;
        import org.apache.ibatis.session.SqlSessionFactory;
        import org.apache.ibatis.session.SqlSessionFactoryBuilder;

        import java.io.IOException;
        import java.io.Reader;
        import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = build.openSession();

        List<User> users = session.selectList("findAll");
        System.out.println(users);
        //session.delete("deleteById",5);
        session.update("updateById", new User(4, "xx", "xx"));
        //session.insert("insert",new User(4,"x","x"));
        List<User> user1 = session.selectList("findAll");
        System.out.println(user1);

        session.commit();
        session.close();


    }
}

  1. sqlMapConfig.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.cj.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/zjgm?serverTimezone=CTT"></property>
                <property name="username" value="root"></property>
                <property name="password" value="123456"></property>
            </dataSource>
        </environment>
    </environments>
<mappers>
    <mapper resource="com/zhongruan/dao/UserMapper1.xml"></mapper>
</mappers>

</configuration>

你可能感兴趣的:(mybatis)