Java教程

Mybatis初学习

本文主要是介绍Mybatis初学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mybatis初学习

1.导入maven mybatis包依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.6</version>
</dependency>
2.在resources创建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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
</configuration>

在xml文件中,&符号要用&amp;代替

3.创建mybatis工具类,获取SqlSession
public class MybatisUtils {
    private static SqlSessionFactory sqlSessionFactory = null;

    static{
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    public static SqlSession getSqlSession(){
        return sqlSessionFactory.openSession();
    }
}
4.创建user实体类
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 String getName() {
        return name;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                '}';
    }
}
5.创建UserMapper接口,即是原来的dao接口
public interface UserMapper {
    //查询所有用户
    List<User> getUserList();
    //查询指定用户
    User getUser(int id);
    //增加用户
    int insertUser(User user);
    //修改用户
    int updateUser(User user);
    //删除用户
    int deleteUser(int id);
}
6.创建UserMapper对应的xml配置文件,即是原来UserDao接口的实现类
<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--每一个mapper都要配置-->
<mapper namespace="com.sise.dao.UserMapper">
    <select id="getUserList" resultType="com.sise.entity.User">
        select * from `user`
    </select>
    <select id="getUser" resultType="com.sise.entity.User">
        select * from `user` where `id`=#{id}
    </select>
    <insert id="insertUser" parameterType="com.sise.entity.User">
        insert into `user` value (#{id},#{name},#{pwd})
    </insert>
    <update id="updateUser" parameterType="com.sise.entity.User">
        update `user` set `name`=#{name},`pwd`=#{pwd} where `id`=#{id}
    </update>
    <delete id="deleteUser" parameterType="int">
        delete from `user` where `id`=#{id}
    </delete>
</mapper>

注意点:有中文注释可能会报错,要设置encoding=“GBK”

7.在mybatis-config.xml添加mapper配置
<mappers>
    <mapper resource="com/sise/dao/UserMapper.xml"/>
</mappers>

resource的路径要用/,如果用com.sise.dao.UserMapper.xml会报错找不到文件

8.编写测试类
public class UserMapperTest {
    @Test
    public void getUserList(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = userMapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        //关闭资源
        sqlSession.close();
    }
    @Test
    public void getUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUser(1);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void insertUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.insertUser(new User(4,"赵六","123456"));
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(4,"秦八","123456"));
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
        sqlSession.commit();
        sqlSession.close();
    }
}
这篇关于Mybatis初学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!