Author:老九 计算机专业 可控之事 沉重冷静 不可控之事 乐观面对 85180586@qq.com 😄 😆 😵 😭 😰 😅 😢 😤 😍 ?? 😎 😩
👍 👎 💯 👏 🔔 🎁 ? 💣 ?? ?? 🌀 🙇 💋 🙏 💦 💩 ?? 💢 ———————————————— 版权声明:本文为CSDN博主「浦上青天」的原创文章
知识点
CRUD
namespace
- 注意namespace的包名要和Dao/mapper接口的包名一致
select
选择,查询语句
- id:就是对应的namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
1.编写接口 2.编写对应的mapper中的sql语句 3.测试
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from mybatis.user where id = #{id}
</select>
Insert
- id:就是对应的namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
1.编写接口 2.编写对应的mapper中的sql语句 3.测试
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user(id ,name, pwd) values(#{id},#{name},#{pwd});
</insert>
update
- id:就是对应的namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
1.编写接口 2.编写对应的mapper中的sql语句 3.测试
<update id="updateUser" parameterType="com.kuang.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
delete
- id:就是对应的namespace中的方法名
- resultType:Sql语句执行的返回值
- parameterType:参数类型
1.编写接口 2.编写对应的mapper中的sql语句 3.测试
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
UserMapper接口
package com.kuang.dao;
import com.kuang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
User getUserById(int id);
int addUser(User user);
int updateUser(User user);
int deleteUser(int id);
}
UserMapper.xml
<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--绑定一个Mapper接口-->
<mapper namespace="com.kuang.dao.UserMapper">
<!--select查询语句,id是重写方法的名字-->
<select id="getUserList" resultType="com.kuang.pojo.User">
select * from mybatis.user
</select>
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<!--对象中的属性可以直接取出来-->
<insert id="addUser" parameterType="com.kuang.pojo.User">
insert into mybatis.user(id ,name, pwd) values(#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="com.kuang.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
</mapper>
测试类
package com.kuang.dao;
import com.kuang.pojo.User;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
}catch (Exception e){
e.printStackTrace();
}finally {
sqlSession.close();
}
}
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
try{
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
}catch(Exception e){
e.printStackTrace();
}
finally {
sqlSession.close();
}
}
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res= mapper.addUser(new User(4,"haha","123333"));
if(res>0){
System.out.println("插入成功!");
}
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.updateUser(new User(4,"hehe","123123"));
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
mapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}
注意
先赞后看,养成习惯!!!^ _ ^??? 每天都更新知识点哦!!! 码字不易,大家的支持就是我坚持下去的动力。点赞后不要忘记关注我哦!
|