Mybatis实现增删改查
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.org.student=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
Customer:
package com.student.po;
public class Customer {
private Integer id;
private String username;
private String jobs;
@Override
public String toString() {
return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
private String phone;
}
CustomerMapper:
<?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="com.student.mapper.CustomerMapper">
<select id="findCustomerById" resultType="com.student.po.Customer" parameterType="Integer">
select * from t_customer where id = #{
id}
</select>
<select id="findCustomerByName" parameterType="String" resultType="com.student.po.Customer">
select * from t_customer where username like '%${value}'
</select>
<insert id="addCustomer" parameterType="com.student.po.Customer">
insert into t_customer (username,jobs,phone)
values(#{
username},#{
jobs},#{
phone})
</insert>
<update id="updateCustomer" parameterType="com.student.po.Customer">
update t_customer set
username = #{
username},jobs = #{
jobs},phone=#{
phone}
where id = #{
id}
</update>
<delete id="deleteCustomer" parameterType="Integer">
delete from t_customer where id=#{
id}
</delete>
</mapper>
mybatis-config:配置文件
<?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>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/student/mapper/CustomerMapper.xml" />
</mappers>
</configuration>
mybatisTest:测试文件
package com.student.test;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.student.po.Customer;
public class MybatisTest {
/*
@Test
public void findCustomerByIdTest() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Customer customer = sqlSession.selectOne("com.student.mapper.CustomerMapper.findCustomerById",1);
System.out.println(customer.toString());
sqlSession.close();
}
*/
/*
@Test
public void findCustomerByNameTest() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Customer> customers = sqlSession.selectList("com.student.mapper.CustomerMapper.findCustomerByName");
for (Customer customer :customers) {
System.out.println(customer);
}
sqlSession.close();
}
*/
/*
@Test
public void addCustomerTest() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Customer customer = new Customer();
customer.setUsername("rose");
customer.setJobs("ITboy");
customer.setPhone("1234567654");
int rows = sqlSession.insert("com.student.mapper.CustomerMapper.addCustomer",customer);
if(rows>0)
{
System.out.println("您已经成功的插入了"+rows+"条数据");
}
else
{
System.out.println("执行插入操作失败!!!!");
}
sqlSession.commit();
sqlSession.close();
}
*/
/*
@Test
public void updateCustomerTest() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
Customer customer = new Customer();
customer.setId(6);
customer.setUsername("rose");
customer.setJobs("programmer");
customer.setPhone("00000000");
int rows = sqlSession.update("com.student.mapper.CustomerMapper.updateCustomer",customer);
if(rows>0)
{
System.out.println("您已经成功的修改了"+rows+"条数据");
}
else
{
System.out.println("执行修改操作失败!!!!");
}
sqlSession.commit();
sqlSession.close();
}
*/
@Test
public void deleteCustomerTest() throws Exception{
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int rows = sqlSession.delete("com.student.mapper.CustomerMapper.deleteCustomer",6);
if(rows>0)
{
System.out.println("您已经成功的删除了"+rows+"条数据");
}
else
{
System.out.println("执行删除操作失败!!!!");
}
sqlSession.commit();
sqlSession.close();
}
}
还没有评论,来说两句吧...