.Mybatis
使用MyBatis对数据库进行增删改查?
查询所有操作?
UserDao接口:
public List<User> findAll();
UserMapper.xml:
<select id="findAll" resultType="com.mybatis.pojo.User">
select * from user
</select>
增加操作?
UserDao接口:
public void add(User user);
UserMapper.xml:
<insert id="add" parameterType="com.mybatis.pojo.User">
insert into user values(null, #{name}, #{age})
</insert>
修改操作?
UserDao接口:
public void update(User user);
UserMapper.xml:
<update id="update" parameterType="com.mybatis.pojo.User">
update user set name = #{name}, age = #{age} where id = #{id}
</update>
删除操作?
UserDao接口:
public void delete(int id);
UserMapper.xml:
<delete id="delete" parameterType="int">
delete from user where id = #{id}
</delete>
根据id查询?
UserDao接口:
public User findById(int id);
UserMapper.xml:
<select id="findById" parameterType="int" resultType="com.mybatis.pojo.User">
select * from user where id = #{id}
</select>
查询总记录数?
UserDao接口:
public int count();
UserMapper.xml:
<select id="count" resultType="int">
select count(*) from user
</select>
根据姓名查找数据(模糊查询)?
方式一:
UserDao接口:
public List<User> findByName(String name);
UserMapper.xml:
<select id="findByName" parameterType="string" resultType="com.mybatis.pojo.User">
select * from user where name like #{name}
</select>
方式二:
UserDao接口:
public List<User> findByName1(String name);
UserMapper.xml:
<select id="findByName1" parameterType="string" resultType="com.mybatis.pojo.User">
select * from user where name like '%${value}%'
</select>
Mybatis操作
最新推荐文章于 2024-06-14 14:08:52 发布