利用Mybatis实现修改功能

2020年12月6日 6点热度 0条评论 来源: dancheng_work

User.java  存储用户的信息
User.xml   配置查询用户的sql语句
SqlMapConfig.xml    加载配置文件,
MybatisFirst.java   数据库操作文件

MybatisFirst.java

public class MybatisFirst {
    @Test
    public void updateUserTest() throws IOException{
        //MyBatis配置文件
        String resource = "SqlMapConfig.xml";
        //得到配置文件流
        InputStream inputStream = Resources.getResourceAsStream(resource);  
            
        //创建会话工厂,想build中传入配置文件信息
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //通过工厂得到SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //list中的User和映射文件中的resultType所指定的类型一致
        User user = new User();
        user.setId(1);
        user.setUsername("dancheng1");
        user.setSex("1");
        user.setBirthday(new Date());
        user.setAddress("吉林建筑大学-ACM协会");
        sqlSession.insert("test.updateUser", user);
        
        sqlSession.commit();
        sqlSession.close();
    }
}

User.java

public class User {
    private int id;
    private String username;//  用户姓名
    private String sex;// 性别
    private Date birthday;// 生日
    private String address;// 地址
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", sex=" + sex
                + ", birthday=" + birthday + ", address=" + address + "]";
    }
    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 getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    
}

User.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="test">
    <update id="updateUser" parameterType="dancheng.mybatis.po.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
    </update>
</mapper>

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>
    <!-- 和spring整合后 environments配置将废除-->
    <environments default="development">
        <environment id="development">
        <!-- 使用jdbc事务管理-->
            <transactionManager type="JDBC" />
        <!-- 数据库连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
                <property name="username" value="root" />
                <property name="password" value="123" />
            </dataSource>
        </environment>
    </environments>
    <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="sqlmap/User.xml"/>
    </mappers>
    
</configuration>
    原文作者:dancheng_work
    原文地址: https://blog.csdn.net/dancheng1/article/details/53965498
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。