mybatis基础操作

测试代码段:

public class MybatisTest {
    private InputStream in = null;
    private SqlSession session =null;
    private IUserDao userDao = null;
    @Before
    public void init() throws Exception{
        //读取配置文件
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //创建一个SqlSessionFactory工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //使用工厂生产一个SQLSession
        session = factory.openSession();
        //使用SQLSession创建Dao接口的代理对象
        userDao = session.getMapper(IUserDao.class);
    }
    @After
    public void destroy() throws Exception{
        session.commit();
        session.close();
        in.close();
    }
    @Test
    public void findAll(){
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void add(){
        User user = new User();
        user.setUsername("lin");
        user.setAddress("郑州市高新区科学大道100号");
        user.setBirthday(new Date());
        userDao.add(user);
    }
    @Test
    public void update(){
        User user = new User();
        user.setId(50);
        user.setSex("男");
        userDao.update(user);
    }
    @Test
    public void delete(){
        userDao.delete(50);
    }
    @Test
    public void selectById(){
        User user = userDao.select(41);
        System.out.println(user);
    }
    @Test
    public void selectByName(){
        String name = "%" + "王" + "%";
        List<User> users = userDao.selectByName(name);
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void findTotal(){
        int total = userDao.findTotal();
        System.out.println(total);
    }
}

vo接口

public class User implements Serializable {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", birthday=" + birthday +
                ", sex='" + sex + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/schema/mybatis-3-mapper.dtd">
<mapper namespace="com.zzu.domain.dao.IUserDao">
    <!--配置查询所有-->
    <select id="findAll" resultType="com.zzu.domain.vo.User">
        select * from user;
    </select>
    <insert id="add" parameterType="com.zzu.domain.vo.User">
        insert into user(username,address,sex,birthday) values(#{username},#{address},#{sex},#{birthday})
    </insert>
    <update id="update" parameterType="com.zzu.domain.vo.User">
        update user set sex=#{sex} where id = #{id}
<!--        <trim prefix="set" suffixOverrides="," suffix="where id=#{id}">-->
<!--            <if test="username != null">username=#{username},</if>-->
<!--            <if test="address != null">address=#{address},</if>-->
<!--            <if test="sex != null">sex=#{sex},</if>-->
<!--            <if test="birthday != null">birthday=#{birthday},</if>-->
<!--        </trim>-->
    </update>
    <delete id="delete" parameterType="int">
        delete from user where id = #{ui}
    </delete>
    <select id="select" parameterType="int" resultType="com.zzu.domain.vo.User">
        select * from user where id=#{id}
    </select>
    <select id="selectByName" parameterType="String" resultType="com.zzu.domain.vo.User">
        select * from user where username like #{name}
    </select>
    <select id="findTotal" resultType="int">
        select count(id) from user
    </select>
</mapper>

接口

public interface IUserDao {
    //查找所有数据
    List<User> findAll();
    //添加数据
    void add(User user);
    //更新数据
    void update(User user);
    //根据id删除用户
    void delete(int id);
    //根据id查询用户信息
    User select(int id);
    //根据名称模糊查询
    List<User> selectByName(String name);
    //查询总用户数
    int findTotal();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值