mybatis simple entry project

Mybatis

1. Mybatis introduction details

​ myBatis is an excellent persistence layer framework that supports ordinary SQL queries, stored procedures and advanced mapping. It supports customized SQL, stored procedures and advanced mapping. MyBatis avoids almost all JDBC code and manual setting of parameters and obtaining result sets. MyBatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (Plain Ordinary Java Objects) into records in the database.

2. Quick start of mybatis

Take an example of adding and modifying user information as an example

Project catalog introduction

Insert picture description here

2.1 add jar package

Add jar package in conf.xml

Add mybatis

Insert picture description here

Add mysql driver package

Insert picture description here

Add lombok

Insert picture description here

2.2 Database construction and table creation

Insert picture description here

2.3 Add the configuration file conf.xml of mybatis

<?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.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/qy129?useSSL=false&amp;characterEncoding=utf8&amp;useUnicode=true" />
                <property name="username" value="root" />
                <property name="password" value="283003" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>
</configuration>

2.4 Define the corresponding entity class in the table

package aaa.qy129.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data //帮你生成set 和 get 以及toString
@NoArgsConstructor //无参构造函数
@AllArgsConstructor //全参构造函数 //必须安装lombok插件
public class User {
    
    
    private int id;
    private String name;
    private String sex;


}

2.5 Define the SQL mapping file for operating the user table

userMapper.xml file

<?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="mao">
    <!--parameterType 参数类型 resultType="aaa.qy129.entity.User"返回的结果集类型-->
    <select id="getUser" parameterType="int"  resultType="aaa.qy129.entity.User">
       select * from user
    </select>
    <insert id="addUser" >
        insert into user(name,sex) value(#{
    
    name},#{
    
    sex})
    </insert>
    <delete id="delUser">
        delete from user where id=#{
    
    id}
    </delete>
    <update id="updateUser" parameterType="aaa.qy129.entity.User">
        update user set  name=#{
    
    name},sex=#{
    
    sex} where id=#{
    
    id}
    </update>
</mapper>

2.6 Register the userMapper.xml file in the conf.xml file

 <mappers>
        <mapper resource="mapper/UserMapper.xml"/>
    </mappers>

2.7. Write test code:

 //加载mybatis的配置文件
        Reader reader = Resources.getResourceAsReader("conf.xml");
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能映射文件中的sql的sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        List<Object> objects = session.selectList("mao.getUser");
        //1.查询新增
        System.out.println(objects);
        //session.insert("mao.addUser",new User(1,"mao","add"));
        //2.添加
        //session.insert("mao.addUser",new User(1,"mao19","add"));
        //3.修改
        //int update = session.update("mao.updateUser", new User(1, "毛云飞天下第一帅", "男神"));
        //System.out.println(update);
        //4.删除
        //int delete = session.delete("mao.delUser",7);
        //System.out.println(objects);
        //事务都是默认开启,需要手动提交commit
        session.commit();

Guess you like

Origin blog.csdn.net/weixin_42550986/article/details/115018299