最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

Mybatis基本使用

来源:博客园


(相关资料图)

1、创建maven项目,导入mybatis依赖。

            org.mybatis            mybatis            3.5.4.SNAPSHOOT        

2、创建MybatisConfig.xml配置连接文件

                                                                                                                                                                            

3、创建User用户实体类

@Data@Builder@NoArgsConstructor@AllArgsConstructorpublic class User {    private Long id;    /**     * 名称     */    private String name;    /**     * 邮箱     */    private String email;    /**     * 手机号     */    private String phoneNumber;}

4、创建用户model接口

@Mapperpublic interface UserMapper {    List getPageUser(@Param("name") String name);    void insertUser(User user);    void updateUser(User user);    void deletetUser(Long userId);}

5、创建映射文件UserMapper.xml

                                INSERT INTO `tb_user`        (`name`, `account`,`password`, `email`,phone_number,remark)        VALUES        (#{name}, #{account}, #{password}, #{email},#{phoneNumber},#{remark});                UPDATE tb_user                                    name = #{name},                                        account = #{account},                                        password = #{password},                                        email = #{email},                                        phone_number = #{phoneNumber},                                        remark = #{remark},                            where id = #{id}                DELETE FROM tb_user WHERE id = #{userId}    

6、在测试类中编写测试文件MTest.java

public class Test {    InputStream inputStream;    SqlSession sqlSession;    @Before    public void beforeSql() throws IOException {        inputStream = Resources.getResourceAsStream("MybatisConfig.xml");        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);        sqlSession = sqlSessionFactory.openSession();    }    @org.junit.Test    public void querySql(){        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        List pageUser = userMapper.getPageUser(null);        for (int i = 0; i < pageUser.size(); i++) {            System.out.println(pageUser.get(i));        }    }    @org.junit.Test    public void insertSql(){        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        User user=new User();        user.setAccount("aaaaa@qq.com");        user.setEmail("aaaaa@qq.com");        user.setName("掌声");        user.setPassword("SDWAR!@");        user.setPhoneNumber("12345");        user.setRemark("测试优化信息");        userMapper.insertUser(user);        sqlSession.commit();        System.out.println();    }    @org.junit.Test    public void updateSql(){        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        User user=new User();        user.setAccount("bbabbb@qq.com");        user.setEmail("bbbabb@qq.com");        user.setName("掌声bb");        user.setPassword("SDbbWAR!@");        user.setPhoneNumber("12345");        user.setRemark("测试优化b信息");        user.setId(5L);        userMapper.updateUser(user);        System.out.println();        sqlSession.commit();    }    @org.junit.Test    public void deleteSql(){        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        userMapper.deletetUser(6L);        System.out.println();        sqlSession.commit();    }    @After    public void afterSql() throws IOException {        sqlSession.close();        inputStream.close();    }}
View Code

关键词: