Mybatis入门增删改查学习笔记

1、什么是Mybatis

Mybatis是基于java的持久层框架,它内部封装了jdbc,使开发者只需要关注sql语句本身

通过xml或注解的方式将要执行的各种 statement配置起来,并通过java对象和statement中sql的动态参数进行映射生成最终执行的sql语句。

最后mybatis框架执行sql并将结果映射为java对象并返回。

2、MyBatis开发入门

2.1添加MyBatis的坐标

<dependencies>
    <dependency>
        <groupId>mysqlgroupId>
        <artifactId>mysql-connector-javaartifactId>
        <version>5.1.32version>
    dependency>
    <dependency>
        <groupId>org.mybatisgroupId>
        <artifactId>mybatisartifactId>
        <version>3.4.5version>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
    <dependency>
        <groupId>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>1.2.17version>
    dependency>
dependencies>

2.2创建user数据表

Mybatis入门增删改查学习笔记_第1张图片

2.3编写User实体类

public class User {
    private int id;
    private String username;
    private String password;
    //省略set、get、tostring方法
}

2.4编写映射文件UserMapper.xml





<mapper namespace="userMapper">

    
    
    <select id="findAll" resultType="user">
        select * from user
    select>

    
    
    <insert id="save" parameterType="user">
        insert into user values(#{id},#{username},#{password});
    insert>

    
    <update id="update" parameterType="user">
        update user set username=#{username},password=#{password} where id = #{id};
    update>
    
    
    <delete id="delete" parameterType="int">
        delete from user where id = #{id};
    delete>
mapper>

2.5编写核心文件SqlMapConfig.xml



<configuration>

    
    <properties resource="jdbc.properties">properties>

    
    <typeAliases>
        <typeAlias type="cn.guixinchn.domain.User" alias="user"/>
    typeAliases>

    
    <environments default="developement">
        <environment id="developement">
            <transactionManager type="JDBC">transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
        <mapper resource="cn/guixinchn/mapper/UserMapper.xml">mapper>
    mappers>
configuration>

jdbc.properties

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = 123456

2.5编写测试类

@Test
public void test1() throws IOException {
     
    
    //模拟数据
    User user = new User();
    user.setUsername("测试");
    user.setPassword("123");
    
    
    // 获取核心配置文件
    InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 获取session工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
    //获得session回话对象
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //执行操作 参数: namespace+id
    
    //查询所有
    //List userList = sqlSession.selectList("userMapper.findAll");
    //打印数据
    //System.out.println(userList);
    
    //插入操作
    //sqlSession.insert("userMapper.save",user);
    //提交数据
    //sqlSession.commit();
    
    //修改操作
    //sqlSession.update("userMapper.update",user);
    //sqlSession.commit();
    
    //删除操作
    //sqlSession.delete("userMapper.delete",4);
    //sqlSession.commit();
    
    //释放资源
    sqlSession.close();
}

你可能感兴趣的:(#,Mybatis,编程语言,mybatis,java)