标签:为什么 dex 灵活 data develop 不能 use his index
如何获取Mybatis?
GitHub 源码:https://github.com/mybatis/mybatis-3/releases
中文文档:https://mybatis.org/mybatis-3/zh/index.html
对应的 maven 依赖
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.2</version>
</dependency>数据持久化
Dao 层
mybatis-confgi.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"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
      <mapper resource="com/youzi/dao/UserMapper.xml"/>
  </mappers>
</configuration>
public class MybatisUtils {
    
    private static SqlSessionFactory sqlSessionFactory;
    
    static {
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
package com.youzi.pojo;
public class User {
    private int id;
    private String name;
    private String pwd;
    public User() {
    }
    public User(int id, String name, String pwd) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    ...
}
public interface UserMapper {
    List<User> getUserList();
    User getUserById(int uId);
    int addUser(User user);
    int updateUser(User user);
    int deleteUser(int id);
}
<?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.youzi.dao.UserDao">
    <select id="getUserList" resultType="com.youzi.pojo.User">
        select * from mybatis_db.user;
    </select>
</mapper>
<select id="getUserList" resultType="com.youzi.pojo.User">
    select * from mybatis_db.user;
</select>
<select id="getUserById" parameterType="int" resultType="com.youzi.pojo.User">
    select * from user where id = #{uId};
</select>
@Test
public void test_getUser() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    List<User> userList = mapper.getUserList();
    for (User user : userList) {
        System.out.println(user);
    }
    sqlSession.close();
}
@Test
public void test_getUserById() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    User user = mapper.getUserById(1);
    System.out.println(user);
    sqlSession.close();
}
修改,添加,删除因为有事务的存在所以必须 commit
<insert id="addUser" parameterType="com.youzi.pojo.User">
    insert into user (id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
 @Test
public void test_addUser() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    int i = mapper.addUser(new User(4, "王五", "2323223"));
    System.out.println(i);
    //重要
    sqlSession.commit();
    sqlSession.close();
}
<update id="updateUser" parameterType="com.youzi.pojo.User">
    update user set name = #{name},pwd = #{pwd} where id = #{id};
</update>
@Test
public void test_updateUser() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    int i = mapper.updateUser(new User(2, "李四", "123123"));
    System.out.println(i);
    sqlSession.commit();
    sqlSession.close();
}
<delete id="deleteUser" parameterType="int">
    delete from user where id = #{id};
</delete>
@Test
public void test_deleteUser() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    int i = mapper.deleteUser(4);
    System.out.println(i);
    sqlSession.commit();
    sqlSession.close();
}
上面的 sqlSession 也可以使用 try() 语句创建 即 try-with-resources 自动关闭资源
try(SqlSession sqlSession = MybatisUtils.getSqlSession()){
    ...
}
org.apache.ibatis.binding.BindingException: Type interface com.youzi.dao.UserDao is not known to the MapperRegistry. 没有绑定 mapper
<mappers>
    <mapper resource="com/youzi/dao/UserMap.xml"/>
</mappers>
idea 放在 resources 目录下报错 java.io.IOException: Could not find resource mybatis-config.xml
新建一个文件目录指定为资源目录然后把
mybatis-config.xml放到这个下面再运行就好了。只要命名为 resources 就不能正常读取资源。。
在 idea 下搭建的第一个MyBatis项目及增删改查用法
标签:为什么 dex 灵活 data develop 不能 use his index
原文地址:https://www.cnblogs.com/wangjr1994/p/12445737.html