本文共 10528 字,大约阅读时间需要 35 分钟。
输出日志信息
# Global logging configurationlog4j.rootLogger=ERROR, stdout# MyBatis logging configuration...log4j.logger.com.yzb.chapter06=DEBUG# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在这里插入代码片package com.yzb.chapter06.pojo;/** 客户持久化层* */public class Customer { private Integer id; private String username; private String jobs; private String 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; } @Override public String toString() { return "Customer{" + "id=" + id + ", username='" + username + '\'' + ", jobs='" + jobs + '\'' + ", phone='" + phone + '\'' + '}'; }}
package com.yzb.chapter06;import com.yzb.chapter06.pojo.Customer;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 java.io.IOException;import java.io.InputStream;import java.util.List;/** 入门程序* */public class Test {/* public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 Customer customer = sqlSession.selectOne("com.yzb.chapter06.mapper.CustomerMapper.findCustomerById", 1); //打印输出结果 System.out.println(customer.toString()); //关闭SqlSession sqlSession.close(); }*/ public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 Listcustomers = sqlSession.selectList("com.yzb.chapter06.mapper.CustomerMapper.findCustomerByUsername", "j"); //打印输出结果## for (Customer customer : customers) { System.out.println(customer.toString()); } //关闭SqlSession sqlSession.close(); }}
在上面的代码基础上进行更改。
insert into t_customer(username,jobs,phone) values(#{username},#{jobs},#{phone}) update t_customer set username= #{username} ,jobs = #{jobs} ,phone = #{phone} where id = #{id} delete from t_customer where id = #{id}
package com.yzb.chapter06;import com.yzb.chapter06.pojo.Customer;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 java.io.IOException;import java.io.InputStream;import java.util.List;/** 入门程序* */public class Test { /* * 查询用户 * *//* public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 Customer customer = sqlSession.selectOne("com.yzb.chapter06.mapper.CustomerMapper.findCustomerById", 1); //打印输出结果 System.out.println(customer.toString()); //关闭SqlSession sqlSession.close(); }*//** 模糊查询用户* */ /* public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 Listcustomers = sqlSession.selectList("com.yzb.chapter06.mapper.CustomerMapper.findCustomerByUsername", "j"); //打印输出结果 for (Customer customer : customers) { System.out.println(customer.toString()); } //关闭SqlSession sqlSession.close(); }*/ /* * 添加用户 * *//* public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //创建一个Customer对象,并向对象中添加数据 Customer customer = new Customer(); customer.setUsername("yue"); customer.setJobs("147"); customer.setPhone("147"); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 //第一个参数是Sql语句的id,第二个参数是sql语句执行时要添加的参数 int count = sqlSession.insert("com.yzb.chapter06.mapper.CustomerMapper.addCustomer", customer); if(count>0){ System.out.println("插入数据成功"); }else { System.out.println("插入数据失败"); } //提交事务,对于曾删改需要提交事务,否则更改不了数据库 sqlSession.commit(); //关闭SqlSession sqlSession.close(); }*//** 修改用户* *//* public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //创建一个Customer对象,并向对象中添加数据 Customer customer = new Customer(); customer.setId(4); customer.setUsername("yue"); customer.setJobs("369"); customer.setPhone("369"); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 //第一个参数是Sql语句的id,第二个参数是sql语句执行时要添加的参数 int update = sqlSession.update("com.yzb.chapter06.mapper.CustomerMapper.updateCustomer", customer); if(update>0){ System.out.println("修改成功"); }else { System.out.println("修改失败"); } //提交事务,对于曾删改需要提交事务,否则更改不了数据库 sqlSession.commit(); //关闭SqlSession sqlSession.close(); }*//** 删除用户* */ public static void main(String[] args) throws IOException { //读取配置文件 String resource = "com/yzb/chapter06/mybatis-config.xml"; //得到文件的输出流 InputStream resourceAsStream = Resources.getResourceAsStream(resource); //根据配置文件构建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //通过SqlSessionFactory创建Sqlsession SqlSession sqlSession = sessionFactory.openSession(); //sqlSession执行映射文件中定义的SQL语句,并返回映射结果 //第一个参数是Sql语句的id,第二个参数是sql语句执行时要添加的参数 int delete = sqlSession.delete("com.yzb.chapter06.mapper.CustomerMapper.deleteCustomer", 4); if(delete>0){ System.out.println("成功"); }else { System.out.println("失败"); } //提交事务,对于曾删改需要提交事务,否则更改不了数据库 sqlSession.commit(); //关闭SqlSession sqlSession.close(); }}
转载地址:http://czwki.baihongyu.com/