Mybatis实现增删改查

水深无声 2022-04-10 07:59 508阅读 0赞

首先往pom文件中加入配置以获取jar包

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.11</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
  8. <dependency>
  9. <groupId>org.mybatis</groupId>
  10. <artifactId>mybatis</artifactId>
  11. <version>3.4.6</version>
  12. </dependency>
  13. <!--mysql-->
  14. <dependency>
  15. <groupId>mysql</groupId>
  16. <artifactId>mysql-connector-java</artifactId>
  17. <version>5.1.47</version>
  18. </dependency>

编写一个Mybatis的主配置文件

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--配置环境-->
  7. <environments default="development">
  8. <environment id="development">
  9. <!--jdbc事务-->
  10. <transactionManager type="JDBC"/>
  11. <!--连接池-->
  12. <dataSource type="POOLED">
  13. <!--驱动-->
  14. <property name="driver" value="com.mysql.jdbc.Driver"/>
  15. <!--数据库链接-->
  16. <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8"/>
  17. <!--用户名-->
  18. <property name="username" value="root"/>
  19. <!--密码-->
  20. <property name="password" value="root"/>
  21. </dataSource>
  22. </environment>
  23. </environments>
  24. <mappers>
  25. <!--映射文件-->
  26. <mapper resource="com/hw/entity/Student.xml"/>
  27. </mappers>
  28. </configuration>

创建实体类和实体类的配置文件

  1. package com.hw.entity;
  2. /**
  3. * @program: Maven
  4. * @description:
  5. * @author: hw
  6. * @create: 2019-01-01 00:42
  7. **/
  8. public class Student {
  9. private Integer sid;
  10. private String sname;
  11. private Integer tid;
  12. public Student() {
  13. super();
  14. }
  15. public Student(String sname, Integer tid) {
  16. this.sname = sname;
  17. this.tid = tid;
  18. }
  19. public Student(Integer sid, String sname, Integer tid) {
  20. this.sid = sid;
  21. this.sname = sname;
  22. this.tid = tid;
  23. }
  24. @Override
  25. public String toString() {
  26. return "Student{" +
  27. "sid=" + sid +
  28. ", sname='" + sname + '\'' +
  29. ", tid=" + tid +
  30. '}';
  31. }
  32. public Integer getSid() {
  33. return sid;
  34. }
  35. public void setSid(Integer sid) {
  36. this.sid = sid;
  37. }
  38. public String getSname() {
  39. return sname;
  40. }
  41. public void setSname(String sname) {
  42. this.sname = sname;
  43. }
  44. public Integer getTid() {
  45. return tid;
  46. }
  47. public void setTid(Integer tid) {
  48. this.tid = tid;
  49. }
  50. }
  51. <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper
  52. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  53. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  54. <mapper namespace="student">
  55. <!--根据id查-->
  56. <!-- id statementid 唯一
  57. parameterType 传入类型
  58. resultType 单个查询返回结果类型
  59. #{} 一个占位符
  60. #{id} 该占位符等待接收参数的名称为Id
  61. -->
  62. <select id="findStudentById" resultType="com.hw.entity.Student" parameterType="int">
  63. select * from student where sid=#{id}
  64. </select>
  65. <!--根据名字模糊查询-->
  66. <!--
  67. ${} 表示拼接sql字符串
  68. ${value} 表示拼接的是简单类型参数
  69. 1.如果参数为简单类型时,${}里面的参数名称必须为value 固定写法
  70. 2.${}为引起sql注入,一般不推荐使用,但某些情况下必须使用,如order by ${name}
  71. -->
  72. <select id="findStudentByName" parameterType="String" resultType="com.hw.entity.Student">
  73. select * from student where sname like '%${value}%'
  74. </select>
  75. <!--添加-->
  76. <!--占位符对应对象属性-->
  77. <insert id="addStudent" parameterType="com.hw.entity.Student">
  78. insert into student (sname,tid)
  79. values (#{sname},#{tid});
  80. </insert>
  81. <!--删除-->
  82. <delete id="delStudent" parameterType="int">
  83. delete from student where sid =#{id}
  84. </delete>
  85. <!--修改-->
  86. <update id="editStudent" parameterType="com.hw.entity.Student">
  87. update student set sname=#{sname},tid=#{tid} where sid =#{sid};
  88. </update>
  89. </mapper>

测试:

  1. package com.hw.test;
  2. import com.hw.entity.Student;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.junit.After;
  8. import org.junit.Before;
  9. import org.junit.Test;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.List;
  13. /**
  14. * @program: Maven
  15. * @description:
  16. * @author: hw
  17. * @create: 2019-01-01 01:12
  18. **/
  19. public class Demo1 {
  20. InputStream inputStream;
  21. SqlSessionFactory build;
  22. SqlSession sqlSession;
  23. @Before
  24. public void before() {
  25. try {
  26. //读取配置文件
  27. inputStream = Resources.getResourceAsStream("Mybatis.xml");
  28. //通过sqlsessionFactoryBuilder创建sqlsessionFactory会话工厂
  29. build = new SqlSessionFactoryBuilder().build(inputStream);
  30. //通过sqlsessionFactory创建sqlsession
  31. sqlSession = build.openSession();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. @After
  37. public void after() {
  38. //关闭sqlsession
  39. sqlSession.commit();
  40. sqlSession.close();
  41. }
  42. /**
  43. * @Description: 查询
  44. * @Param: []
  45. * @return: void
  46. * @Author: hw
  47. * @Date: 2019/1/1
  48. */
  49. @Test
  50. public void test1() throws IOException {
  51. //调用sqlsession的操作数据库的方法
  52. // Student student=sqlSession.selectOne("findStudentById",1);
  53. List<Student> findStudentByName = sqlSession.selectList("findStudentByName", "1");
  54. findStudentByName.forEach(x -> System.out.println(x));
  55. //关闭sqlsession
  56. sqlSession.close();
  57. }
  58. /**
  59. * @Description: 添加
  60. * @Param: []
  61. * @return: void
  62. * @Author: hw
  63. * @Date: 2019/1/1
  64. */
  65. @Test
  66. public void test2() {
  67. Student student = new Student("猴子", 1);
  68. int addStudent = sqlSession.insert("addStudent", student);
  69. System.out.println(addStudent);
  70. }
  71. /**
  72. * @Description: 删除
  73. * @Param: []
  74. * @return: void
  75. * @Author: hw
  76. * @Date: 2019/1/1
  77. */
  78. @Test
  79. public void test3() {
  80. System.out.println(sqlSession.delete("delStudent", 2));
  81. }
  82. /**
  83. * @Description: 修改
  84. * @Param:
  85. * @return:
  86. * @Author: hw
  87. * @Date: 2019/1/1
  88. */
  89. @Test
  90. public void test4(){
  91. Student student=new Student(1,"李箐",2);
  92. System.out.println(sqlSession.update("editStudent",student));
  93. }
  94. }

" class="reference-link">结果 20190101013025928.png

发表评论

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

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

相关阅读