20220629_MyBatis_数据库增删改查练习(四)

文章目录


按照id删除单个记录
接口建方法

void deleteById(int id);

生成映射语句

    <delete id="deleteById">
        delete from mybatis.brand
            where id = #{id}
    </delete>

执行代码

    @Test
    public void testDelById() throws IOException {
   
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个关于MyBatis的完整的增删改查练习题: 1. 创建一个名为`User`的Java类,它具有以下属性: ``` private int id; private String name; private String email; private String password; ``` 2. 创建一个名为`UserMapper`的MyBatis Mapper接口,它具有以下方法签名: ``` public List<User> findAll(); public User findById(int id); public void insert(User user); public void update(User user); public void delete(int id); ``` 3. 创建一个名为`UserMapper.xml`的MyBatis Mapper 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="com.example.UserMapper"> <select id="findAll" resultType="com.example.User"> SELECT * FROM user; </select> <select id="findById" resultType="com.example.User"> SELECT * FROM user WHERE id=#{id}; </select> <insert id="insert" parameterType="com.example.User"> INSERT INTO user (name, email, password) VALUES (#{name}, #{email}, #{password}); </insert> <update id="update" parameterType="com.example.User"> UPDATE user SET name=#{name}, email=#{email}, password=#{password} WHERE id=#{id}; </update> <delete id="delete" parameterType="int"> DELETE FROM user WHERE id=#{id}; </delete> </mapper> ``` 4. 创建一个名为`UserDao`的Java类,它包含以下方法: ``` public List<User> findAll() throws IOException; public User findById(int id) throws IOException; public void insert(User user) throws IOException; public void update(User user) throws IOException; public void delete(int id) throws IOException; ``` 这些方法使用`SqlSession`对象从`UserMapper`接口中调用相应的方法。 5. 创建一个名为`MyBatisConfig.xml`的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/mydatabase" /> <property name="username" value="root" /> <property name="password" value="mypassword" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/example/UserMapper.xml" /> </mappers> </configuration> ``` 6. 在`main`方法中,创建一个`SqlSessionFactory`对象,使用它创建一个`SqlSession`对象。然后,使用`UserDao`对象从数据库中执行一些操作,例如: ``` SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("MyBatisConfig.xml")); SqlSession session = sessionFactory.openSession(); UserDao userDao = session.getMapper(UserDao.class); List<User> userList = userDao.findAll(); User user = userDao.findById(1); userDao.insert(new User("John Doe", "john.doe@example.com", "password123")); userDao.update(new User(1, "Jane Doe", "jane.doe@example.com", "password456")); userDao.delete(2); session.commit(); session.close(); ``` 这些操作将查询所有用户,查询ID为1的用户,插入一个新用户,更新ID为1的用户,删除ID为2的用户,并提交更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值