• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Spring Boot结合Mybatis的项目搭建和数据库增删改查操作

武飞扬头像
抱着电脑入睡
帮助1

引入依赖

  1.  
    <dependency>
  2.  
    <groupId>org.mybatis</groupId>
  3.  
    <artifactId>mybatis</artifactId>
  4.  
    <version>3.3.0</version>
  5.  
    </dependency>
  6.  
    <dependency>
  7.  
    <groupId>mysql</groupId>
  8.  
    <artifactId>mysql-connector-java</artifactId>
  9.  
    <version>8.0.15</version>
  10.  
    </dependency>
  11.  
    <dependency>
  12.  
    <groupId>log4j</groupId>
  13.  
    <artifactId>log4j</artifactId>
  14.  
    <version>1.2.16</version>
  15.  
    </dependency>
  16.  
    <dependency>
  17.  
    <groupId>org.slf4j</groupId>
  18.  
    <artifactId>slf4j-api</artifactId>
  19.  
    <version>1.6.1</version>
  20.  
    </dependency>
  21.  
    <dependency>
  22.  
    <groupId>org.slf4j</groupId>
  23.  
    <artifactId>slf4j-nop</artifactId>
  24.  
    <version>1.6.4</version>
  25.  
    </dependency>
  26.  
    <dependency>
  27.  
    <groupId>org.junit.jupiter</groupId>
  28.  
    <artifactId>junit-jupiter-api</artifactId>
  29.  
    <version>5.8.2</version>
  30.  
    <scope>test</scope>
  31.  
    </dependency>
学新通

在pom.xml文件build标签中添加

  1.  
    <resources>
  2.  
    <!-- 编译之后包含xml -->
  3.  
    <resource>
  4.  
    <directory>src/main/java</directory>
  5.  
    <includes>
  6.  
    <include>**/*.xml</include>
  7.  
    </includes>
  8.  
    <filtering>true</filtering>
  9.  
    </resource>
  10.  
    </resources>

在resource目录下创建log4j.propertise

  1.  
    spring.datasource.filters=stat,wall,log4j
  2.  
    # Global logging configuration
  3.  
    log4j.rootLogger=ERROR,stdout
  4.  
    # MyBatis logging configuration...
  5.  
    log4j.logger.net.biancheng=DEBUG
  6.  
    # Console output...
  7.  
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  8.  
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  9.  
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
  10.  
     

在resource目录下创建mybatis-config.xml文件

  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.  
     
  6.  
    <settings>
  7.  
    <setting name="logImpl" value="LOG4J" />
  8.  
    </settings>
  9.  
     
  10.  
    <!-- 配置mybatis运行环境 -->
  11.  
    <environments default="development">
  12.  
     
  13.  
    <environment id="development">
  14.  
    <!-- 使用JDBC的事务管理 -->
  15.  
    <transactionManager type="JDBC" />
  16.  
    <dataSource type="POOLED">
  17.  
    <!-- MySQL数据库驱动 -->
  18.  
    <property name="driver" value="com.mysql.cj.jdbc.Driver" />
  19.  
    <!-- 连接数据库的URL -->
  20.  
    <property name="url" value="jdbc:mysql://localhost:3306/exam?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT+8&amp;useSSL=false" />
  21.  
    <property name="username" value="root" />
  22.  
    <property name="password" value="root" />
  23.  
    </dataSource>
  24.  
    </environment>
  25.  
    </environments>
  26.  
     
  27.  
    </configuration>
学新通

(改成自己的数据库名字,用户名和密码)搭建就完成啦

整体结构如图

学新通

实体类Student继承Serializable

  1.  
     
  2.  
    public class Student implements Serializable {
  3.  
    private Integer id;
  4.  
    private String age;
  5.  
    private String sex;
  6.  
    private String name;
  7.  
    private String id_card;
  8.  
    private String student_class;
  9.  
    public Student(){
  10.  
     
  11.  
    }
  12.  
     
  13.  
    public Student(Integer id, String age, String sex, String name, String id_card, String student_class) {
  14.  
    this.id = id;
  15.  
    this.age = age;
  16.  
    this.sex = sex;
  17.  
    this.name = name;
  18.  
    this.id_card = id_card;
  19.  
    this.student_class = student_class;
  20.  
    }
  21.  
     
  22.  
    public Integer getId() {
  23.  
    return id;
  24.  
    }
  25.  
     
  26.  
    public void setId(Integer id) {
  27.  
    this.id = id;
  28.  
    }
  29.  
     
  30.  
    public String getAge() {
  31.  
    return age;
  32.  
    }
  33.  
     
  34.  
    public void setAge(String age) {
  35.  
    this.age = age;
  36.  
    }
  37.  
     
  38.  
    public String getSex() {
  39.  
    return sex;
  40.  
    }
  41.  
     
  42.  
    public void setSex(String sex) {
  43.  
    this.sex = sex;
  44.  
    }
  45.  
     
  46.  
    public String getName() {
  47.  
    return name;
  48.  
    }
  49.  
     
  50.  
    public void setName(String name) {
  51.  
    this.name = name;
  52.  
    }
  53.  
     
  54.  
    public String getId_card() {
  55.  
    return id_card;
  56.  
    }
  57.  
     
  58.  
    public void setId_card(String id_card) {
  59.  
    this.id_card = id_card;
  60.  
    }
  61.  
     
  62.  
    public String getStudent_class() {
  63.  
    return student_class;
  64.  
    }
  65.  
     
  66.  
    public void setStudent_class(String student_class) {
  67.  
    this.student_class = student_class;
  68.  
    }
  69.  
     
  70.  
    @Override
  71.  
    public String toString() {
  72.  
    return "Student{"
  73.  
    "id=" id
  74.  
    ", age='" age '\''
  75.  
    ", sex='" sex '\''
  76.  
    ", name='" name '\''
  77.  
    ", id_card='" id_card '\''
  78.  
    ", student_class='" student_class '\''
  79.  
    '}';
  80.  
    }
  81.  
    }
  82.  
     
学新通

查询所有操作 

 dao层

  1.  
     
  2.  
    public interface StudentMapper {
  3.  
    List<Student> selectAllStudent();
  4.  
     
  5.  
    }

mapper文件

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <!DOCTYPE mapper
  3.  
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4.  
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5.  
    <mapper namespace="com.haha.demo.dao.StudentMapper">
  6.  
    <!-- 查询所有网站信息 -->
  7.  
    <select id="selectAllStudent"
  8.  
    resultType="com.haha.demo.pojo.Student">
  9.  
    select * from students
  10.  
    </select>
  11.  
    </mapper>

controller层

  1.  
     
  2.  
    @Controller
  3.  
    public class HelloController {
  4.  
    @RequestMapping("/")
  5.  
    @ResponseBody
  6.  
    public String getHello() throws IOException {
  7.  
    InputStream config = Resources.getResourceAsStream("mybatis-config.xml");
  8.  
    SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(config);
  9.  
    SqlSession ss = ssf.openSession();
  10.  
    StudentMapper pm = ss.getMapper(StudentMapper.class);
  11.  
    List<Student> listWeb = pm.selectAllStudent();
  12.  
    for (Student site : listWeb) {
  13.  
    System.out.println(site);
  14.  
    }
  15.  
    ss.commit();
  16.  
    ss.close();
  17.  
    return "ok!";
  18.  
    }
  19.  
    }
学新通

运行http://localhost:8080/  可以看到控制台的输出结果

删除操作

StudentMapper

  1.  
    public interface StudentMapper {
  2.  
    //查询所有
  3.  
    // List<Student> selectAllStudent();
  4.  
    public void deleteStudent(Integer id);
  5.  
     
  6.  
    }

mapper.xml 

  1.  
    <delete id="deleteStudent" >
  2.  
    delete from students where id=#{id}
  3.  
    </delete>

建个test文件(引入之前加入test依赖)

  1.  
     
  2.  
    public class TestMybatis {
  3.  
    @Test
  4.  
    public void test() throws Exception{
  5.  
    String resource = "mybatis-config.xml";
  6.  
    InputStream inputStream = Resources.getResourceAsStream(resource);
  7.  
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  8.  
    SqlSession session = sqlSessionFactory.openSession(true);
  9.  
    try {
  10.  
     
  11.  
    //Mapper接口:获取Mapper接口的 代理实现类对象
  12.  
    StudentMapper mapper = session.getMapper(StudentMapper.class);
  13.  
     
  14.  
    //4:删除操作
  15.  
    Student student= new Student();
  16.  
    mapper.deleteStudent(104);
  17.  
     
  18.  
    } finally {
  19.  
    session.close();
  20.  
    }
  21.  
    }
  22.  
     
  23.  
    }
学新通

运行test刷新数据库就会看到结果

根据id查询、添加、修改操作

接口

  1.  
     
  2.  
    /**
  3.  
    * 根据id查询
  4.  
     
  5.  
    */
  6.  
    public Student getStudentById(int id);
  7.  
     
  8.  
    /**
  9.  
    * 增加
  10.  
     
  11.  
    */
  12.  
    public Integer insertStudent(Student student);
  13.  
     
  14.  
     
  15.  
    /**
  16.  
    * 修改
  17.  
     
  18.  
    */
  19.  
    public boolean updateStudent(Student student);
学新通

test 

  1.  
    //1: 查询操作调用实际方法实现操作
  2.  
    Student student = mapper.getStudentById(1);
  3.  
    System.out.println(student);
  4.  
     
  5.  
     
  6.  
    //2: 添加操作
  7.  
    Student student = new Student();
  8.  
    student.setLastname("t");
  9.  
    student.setEmail("t@qq.com");
  10.  
    Integer rows = mapper.insertStudent(student);
  11.  
    System.out.println(rows);
  12.  
     
  13.  
     
  14.  
    //3:修改操作
  15.  
    Student student = new Student();
  16.  
    student.setLastname("to");
  17.  
    student.setEmail("t@163.com");
  18.  
    boolean isupdate = mapper.updateStudent(student);
  19.  
    System.out.println(isupdate);
学新通

mapper.xml

  1.  
    <!--查询-->
  2.  
    <select id="getEmployeeById" resultType="com.mybatislearn.entity.Employee">
  3.  
    select * from tbl_employee where id = #{id}
  4.  
    </select>
  5.  
     
  6.  
    <!--增加:useGeneratedKeys和keyProperty属性是设置自增长和自增长的字段的-->
  7.  
    <insert id="insertEmployee" parameterType="com.mybatislearn.entity.Employee" useGeneratedKeys="true" keyProperty="id>
  8.  
    INSERT INTO tbl_employee(lastname,email,gender) VALUES(#{lastname},#{email},#{gender})
  9.  
    </insert>
  10.  
     
  11.  
    <!--修改-->
  12.  
    <update id="updateEmployee" parameterType="com.mybatislearn.entity.Employee">
  13.  
    UPDATE tbl_employee SET lastname=#{lastname},email=#{email},gender=#{gender} WHERE id=#{id}
  14.  
    </update>
  15.  
     
学新通

 运行test刷新数据库就会看到结果

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjhak
系列文章
更多 icon
同类精品
更多 icon
继续加载