Mybatis入门--增删改查

mybatis-config.xml

<?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>
    <!-- 设置别名 -->
    <typeAliases>
        <typeAlias type="org.lezg.web.entity.Customer" alias="Customer"/>
    </typeAliases>

    <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/lezgweb"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 映射文件 -->
    <mappers>
        <mapper resource="mapper/customerMapper.xml"/>
    </mappers>
</configuration>

Entity类:

public class Customer {

    private int id;
    private String name;
    private String contact;
    private String telephone;
    private String email;

    public Customer() {

    }

    public int getId() {
        return id;
    }

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

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

映射文件customerMapper.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="org.lezg.web.mapper.CustomerMapper">
    <!-- 根据id查询Customer-->
    <select id="getCustomerById" parameterType="int" resultType="Customer">
        SELECT * FROM customer WHERE id = #{id}
    </select>

    <!-- 查询所有Custmoer -->
    <select id="getCustomers" resultType="Customer">
        SELECT * FROM customer
    </select>

    <!-- 插入Customer -->
    <insert id="insertCustomer" parameterType="Customer" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO customer (name,contact,telephone,email)
        VALUES (#{name},#{contact},#{telephone},#{email})
    </insert>

    <!-- 更新Customer信息 -->
    <update id="updateCustomer" parameterType="Customer" >
        UPDATE customer set telephone = #{telephone},
        email = #{email} WHERE id = #{id}
    </update>

    <!-- 根据id删除Customer -->
    <delete id="deleteCustomerById" parameterType="int">
        DELETE from customer WHERE id = #{id}
    </delete>
</mapper>

接口编程:

/**
 * 方法名和参数必须与customerMapper.xml中id,parameterType一致
 */
public interface CustomerMapper {

    /**
     * 根据id查询Customer
     *
     * @param id
     * @return Customer
     */
    Customer getCustomerById(int id);

    /**
     * 查询所有Customer
     *
     * @return List<Customer>
     */
    List<Customer> getCustomers();

    /**
     * 插入Customer
     *
     * @param customer
     * @return
     */
    int insertCustomer(Customer customer);

    /**
     * 更新customer by id
     *
     * @param customer
     * @return
     */
    int updateCustomer(Customer customer);

    /**
     * 删除Customer by id
     *
     * @param id
     * @return
     */
    int deleteCustomerById(int id);
}

测试:

public class Test {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init(){
        InputStream in = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("mybatis-config.xml");
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
    }

    private SqlSession getSession() {
        return sqlSessionFactory.openSession();
    }

    private void closeSession(SqlSession session) {
        if (session != null) {
            session.close();
        }
    }

    @org.junit.Test
    public void getCustomer(){
        SqlSession session = getSession();
        CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
        Customer customer = customerMapper.getCustomerById(1);
        System.out.println(customer.getContact());
        System.out.println(customer.getTelephone());
        closeSession(session);
    }

    //@org.junit.Test
    public void addCustomer(){
        SqlSession session = getSession();
        CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
        Customer customer = new Customer();
        customer.setContact("lyj");
        customer.setEmail("lyj@qq.com");
        customer.setName("customer3");
        customer.setTelephone("15151521648");
        int count = customerMapper.insertCustomer(customer);
        System.out.println(count);
        session.commit();
        closeSession(session);
    }

   // @org.junit.Test
    public void updateCustomer(){
        SqlSession session = getSession();
        CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
        Customer customer = new Customer();
        customer.setId(6);
        customer.setTelephone("15151521644");
        customer.setEmail("lyj@gmail.com");
        int count = customerMapper.updateCustomer(customer);
        session.commit();
        System.out.println(count);
    }

    @org.junit.Test
    public void deleteCustomerById(){
        SqlSession session = getSession();
        CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
        int count = customerMapper.deleteCustomerById(7);
        session.commit();
        System.out.println(count);
        closeSession(session);
    }

    @org.junit.Test
    public void getCustomers(){
        SqlSession session = getSession();
        CustomerMapper customerMapper = session.getMapper(CustomerMapper.class);
        List<Customer> customers = customerMapper.getCustomers();
        for (Customer customer : customers) {
            System.out.println(customer.getContact() + " " + customer.getTelephone());
        }
        closeSession(session);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值