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);
}
}