Mybatis实现增删改查

曾经终败给现在 2023-05-31 15:29 93阅读 0赞

log4j.properties

  1. # Global logging configuration
  2. log4j.rootLogger=ERROR, stdout
  3. # MyBatis logging configuration...
  4. log4j.logger.org.student=DEBUG
  5. # Console output...
  6. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  7. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  8. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Customer:

  1. package com.student.po;
  2. public class Customer {
  3. private Integer id;
  4. private String username;
  5. private String jobs;
  6. @Override
  7. public String toString() {
  8. return "Customer [id=" + id + ", username=" + username + ", jobs=" + jobs + ", phone=" + phone + "]";
  9. }
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getUsername() {
  17. return username;
  18. }
  19. public void setUsername(String username) {
  20. this.username = username;
  21. }
  22. public String getJobs() {
  23. return jobs;
  24. }
  25. public void setJobs(String jobs) {
  26. this.jobs = jobs;
  27. }
  28. public String getPhone() {
  29. return phone;
  30. }
  31. public void setPhone(String phone) {
  32. this.phone = phone;
  33. }
  34. private String phone;
  35. }

CustomerMapper:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.student.mapper.CustomerMapper">
  5. <select id="findCustomerById" resultType="com.student.po.Customer" parameterType="Integer">
  6. select * from t_customer where id = #{
  7. id}
  8. </select>
  9. <select id="findCustomerByName" parameterType="String" resultType="com.student.po.Customer">
  10. select * from t_customer where username like '%${value}'
  11. </select>
  12. <insert id="addCustomer" parameterType="com.student.po.Customer">
  13. insert into t_customer (username,jobs,phone)
  14. values(#{
  15. username},#{
  16. jobs},#{
  17. phone})
  18. </insert>
  19. <update id="updateCustomer" parameterType="com.student.po.Customer">
  20. update t_customer set
  21. username = #{
  22. username},jobs = #{
  23. jobs},phone=#{
  24. phone}
  25. where id = #{
  26. id}
  27. </update>
  28. <delete id="deleteCustomer" parameterType="Integer">
  29. delete from t_customer where id=#{
  30. id}
  31. </delete>
  32. </mapper>

mybatis-config:配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  4. <configuration>
  5. <environments default="mysql">
  6. <environment id="mysql">
  7. <transactionManager type="JDBC" />
  8. <dataSource type="POOLED">
  9. <property name="driver" value="com.mysql.jdbc.Driver" />
  10. <property name="url"
  11. value="jdbc:mysql://localhost:3306/mybatis" />
  12. <property name="username" value="root" />
  13. <property name="password" value="123456" />
  14. </dataSource>
  15. </environment>
  16. </environments>
  17. <mappers>
  18. <mapper resource="com/student/mapper/CustomerMapper.xml" />
  19. </mappers>
  20. </configuration>

mybatisTest:测试文件

  1. package com.student.test;
  2. import java.io.InputStream;
  3. import java.util.List;
  4. import org.apache.ibatis.io.Resources;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  8. import org.junit.Test;
  9. import com.student.po.Customer;
  10. public class MybatisTest {
  11. /*
  12. @Test
  13. public void findCustomerByIdTest() throws Exception{
  14. String resource = "mybatis-config.xml";
  15. InputStream inputStream = Resources.getResourceAsStream(resource);
  16. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  17. SqlSession sqlSession = sqlSessionFactory.openSession();
  18. Customer customer = sqlSession.selectOne("com.student.mapper.CustomerMapper.findCustomerById",1);
  19. System.out.println(customer.toString());
  20. sqlSession.close();
  21. }
  22. */
  23. /*
  24. @Test
  25. public void findCustomerByNameTest() throws Exception{
  26. String resource = "mybatis-config.xml";
  27. InputStream inputStream = Resources.getResourceAsStream(resource);
  28. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  29. SqlSession sqlSession = sqlSessionFactory.openSession();
  30. List<Customer> customers = sqlSession.selectList("com.student.mapper.CustomerMapper.findCustomerByName");
  31. for (Customer customer :customers) {
  32. System.out.println(customer);
  33. }
  34. sqlSession.close();
  35. }
  36. */
  37. /*
  38. @Test
  39. public void addCustomerTest() throws Exception{
  40. String resource = "mybatis-config.xml";
  41. InputStream inputStream = Resources.getResourceAsStream(resource);
  42. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  43. SqlSession sqlSession = sqlSessionFactory.openSession();
  44. Customer customer = new Customer();
  45. customer.setUsername("rose");
  46. customer.setJobs("ITboy");
  47. customer.setPhone("1234567654");
  48. int rows = sqlSession.insert("com.student.mapper.CustomerMapper.addCustomer",customer);
  49. if(rows>0)
  50. {
  51. System.out.println("您已经成功的插入了"+rows+"条数据");
  52. }
  53. else
  54. {
  55. System.out.println("执行插入操作失败!!!!");
  56. }
  57. sqlSession.commit();
  58. sqlSession.close();
  59. }
  60. */
  61. /*
  62. @Test
  63. public void updateCustomerTest() throws Exception{
  64. String resource = "mybatis-config.xml";
  65. InputStream inputStream = Resources.getResourceAsStream(resource);
  66. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  67. SqlSession sqlSession = sqlSessionFactory.openSession();
  68. Customer customer = new Customer();
  69. customer.setId(6);
  70. customer.setUsername("rose");
  71. customer.setJobs("programmer");
  72. customer.setPhone("00000000");
  73. int rows = sqlSession.update("com.student.mapper.CustomerMapper.updateCustomer",customer);
  74. if(rows>0)
  75. {
  76. System.out.println("您已经成功的修改了"+rows+"条数据");
  77. }
  78. else
  79. {
  80. System.out.println("执行修改操作失败!!!!");
  81. }
  82. sqlSession.commit();
  83. sqlSession.close();
  84. }
  85. */
  86. @Test
  87. public void deleteCustomerTest() throws Exception{
  88. String resource = "mybatis-config.xml";
  89. InputStream inputStream = Resources.getResourceAsStream(resource);
  90. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  91. SqlSession sqlSession = sqlSessionFactory.openSession();
  92. int rows = sqlSession.delete("com.student.mapper.CustomerMapper.deleteCustomer",6);
  93. if(rows>0)
  94. {
  95. System.out.println("您已经成功的删除了"+rows+"条数据");
  96. }
  97. else
  98. {
  99. System.out.println("执行删除操作失败!!!!");
  100. }
  101. sqlSession.commit();
  102. sqlSession.close();
  103. }
  104. }

发表评论

表情:
评论列表 (有 0 条评论,93人围观)

还没有评论,来说两句吧...

相关阅读