Mybatis基础使用之增删改查、参数配置

1.XML配置实现增删改查

测试时封装代码:

public class Mytest {

    private InputStream in;
    private SqlSession sqlSession;
    private Userdao userdao;
    @Before
    public void init() throws Exception {
        //1.读取配置文件
        in= Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.获取Sqlsession对象
        sqlSession = factory.openSession();
        //4.使用Sqlsession创建接口的代理对象
        userdao = sqlSession.getMapper(Userdao.class);
        //Userdao userdao = sqlSession.getMapper(Userdao.class);
    }
    //@After
    public void des() throws IOException {
        //6.释放资源
        sqlSession.commit();
        sqlSession.close();
        //in.close();
    }
 }

1.1查询(select)
1.1.1查询user表所有用户信息
a.在持久层添加查询方法

public interface Userdao {
    //查找用户所有信息
    List<User> findAll();
}

b.在用户的映射文件配置

<select id="findAll" resultType="com.pojo.User">
        select * from user
    </select>

c.测试

 //查询所有对象
    @Test
    public void testfindall() throws Exception {
        //56.使用代理对象查询所有方法
        List<User> users = userdao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
        des();
    }

d.结果
在这里插入图片描述
1.1.2根据id查询user表中用户信息
a.在持久层添加查询方法

public interface UserDao {
    User findById(Integer integer);
}

b.在用户的映射文件配置

<select id="findById" parameterType="Integer" resultType="com.pojo.User">
        select * from user where id =#{uid}
    </select>

c.测试

//查询指定id
    @Test
    public void testfinbyid() throws Exception {
        //56.使用代理对象查询所有方法
        User user = userdao.findById(52);
        System.out.println(user);
        des();
    }

d.结果
在这里插入图片描述

1.2插入(insert)
a.在持久层添加插入方法

public interface UserDao {
    //插入
    void insertUser(User user);
}

b.在用户的映射文件配置

<insert id="insertUser" parameterType="com.pojo.User">
<selectKey keyColumn="id" keyProperty="id" resultType="int">
    select last_insert_id();
</selectKey>
        insert into user (username,birthday,sex,address)values(#{username},#{birthday},#{sex},#{address})
    </insert>

这样配置可以显示id。
c.测试

//插入数据
    @Test
    public void  testinsertuser() throws Exception {

        User user=new User();
        user.setUsername("liuweiqi");
        user.setSex("女");
        user.setAddress("北京");
        user.setBirthday(new Date());

        userdao.insertUser(user);
        des();
        System.out.println(user);
    }

1.3更新(update)
a.在持久层添加更新方法

public interface UserDao {
    //更新
    int updateUser(User user);
}

b.在用户的映射文件配置

<update id="updateUser" parameterType="com.pojo.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
    </update>

c.测试

  @Test
    public void testupdateUser() throws IOException {
        User user = userdao.findById(52);
        user.setUsername("傻逼");
        user.setSex("公");
        user.setAddress("泰国");
        int res = userdao.updateUser(user);
        System.out.println(res);
        des();
    }

1.4删除(delete)
a.在持久层添加删除方法

public interface UserDao {
    //删除
    int deleteUser(Integer userid);
}

b.在用户的映射文件配置

 <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>

c.测试

//删除指定编号
    @Test
    public void testdeleteUser() throws Exception {
        int res = userdao.deleteUser(51);
        des();
        System.out.println(res);
    }

2.模糊查询、聚合函数

2.1模糊查询第一种方式
#{username}中#{}实现向preparedStatement占位符设置值,自动进行java类型和jdbc类型转换,可以有效防止sql注入。
a.在持久层添加查询方法

public interface UserDao {
    List<User> findByName(String name);
}

b.在用户的映射文件配置

<select id="findByName" parameterType="String" resultType="com.pojo.User">
        select * from user where username like #{username}
    </select>

c.测试

//查询指定名字
    @Test
    public void testfindbyname() throws Exception {
        //56.使用代理对象查询所有方法
        List<User> users = userdao.findByName("%王%");
        for (User user : users) {
            System.out.println(user);
        }
        des();
    }

d.结果
在这里插入图片描述
2.2模糊查询第二种方式

select * from user where username like "%${value}%"

${value}是固定的,基于statement,不进行java和jdbc的类型转换。
2.3查询使用聚合函数
a.在持久层添加查询方法

public interface UserDao {
    //聚合函数查询总数
    int findTotal();
}

b.在用户的映射文件配置

<select id="findTotal" resultType="Integer">
        select count(*) from user;
    </select>

c.测试

//查询总数
    @Test
    public void testfindtotal() throws Exception {
        int res = userdao.findTotal();
        des();
        System.out.println(res);
    }

3.参数配置

3.1parameterType
文档说明:将会传入这条语句的参数的类全限定名或别名。这个属性是可选的,因为 MyBatis 可以通过类型处理器(TypeHandler)推断出具体传入语句的参数,默认值为未设置(unset)。
传递的参数可以是基本类型,也可以是引用类型,还可以是实体类型(如User),还可以是实体类的包装类。如果注册了别名,可以直接用别名。
实体类的包装类:
a.编写QueryVo

public class QueryVo implements Serializable {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

b.在持久层添加查询方法

public interface UserDao {

    //根据QueryVo查询用户信息
    List<User> findByVo(QueryVo vo);
}

b.在用户的映射文件配置

 <select id="findByVo" resultType="user" parameterType="com.pojo.QueryVo">
        select * from user where username like #{user.username}
    </select>

c.测试

//根据Vo查询所有对象
    @Test
    public void testfindbyvo() throws Exception {
        QueryVo vo =new QueryVo();
        User user =new User();
        user.setUsername("%王%");
        vo.setUser(user);
        List<User> users = userdao.findByVo(vo);
        for (User u : users) {
            System.out.println(u);
        }
        des();
    }

d.结果
在这里插入图片描述
3.2resultType
文档说明:期望从这条语句中返回结果的类全限定名或别名。 注意,如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身的类型。 resultType 和 resultMap 之间只能同时使用一个。
支持基本类型和实体类型。如果注册了别名,可以直接用别名。
3.3resultMap
对外部 resultMap 的命名引用。结果映射是 MyBatis 最强大的特性,如果你对其理解透彻,许多复杂的映射问题都能迎刃而解。 resultType 和 resultMap 之间只能同时使用一个。
建立查询的列名和实体类名不一致时建立对应关系。
a.定义resultMap

<resultMap id="usermap" type="com.pojo.User">
        <id column="id" property="userId"></id>
        <result column="username" property="userName"></result>
        <result column="sex" property="userSex"></result>
        <result column="birthday" property="userBirthday">
        <result column="address" property="userAddress"></result>
</resultMap>
    <select id="findAll" resultMap="usermap">
        select * from user
    </select>

4.别名

typeAliases
在SqlMapConfig.xml中配置

    <!--指定别名-->
    <typeAliases>
         <!--单个别名-->
        <typeAlias type="com.pojo.User" alias="user"></typeAlias>
         <!--扫描整个包下的类-->
        <package name="com.pojo"></package>
    </typeAliases>

指定别名后就不用了再写com.pojo.User了,别名就是类名,直接写user,首字母不区分大小写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值