zoukankan      html  css  js  c++  java
  • springboot学习(九) 使用mybatis访问数据库

    1、添加maven依赖

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.1</version>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>

      这里使用的是mysql数据库,如果是其他数据库将数据库依赖换掉即可。

    2、修改配置文件

      在application.properties配置文件中增加mybatis的数据库连接配置:

    1 # 定义注解类所在的包
    2 mybatis.type-aliases-package=cn.origal.domain
    3 
    4 spring.datasource.url=jdbc:mysql://localhost:3309/test?serverTimezone=UTC
    5 spring.datasource.username=root
    6 spring.datasource.password=root
    7 spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    3、修改启动类

      在启动类中增加项目对mapper包的扫描:

     1 @SpringBootApplication
     2 @MapperScan("cn.origal.mapper")
     3 public class Application {
     4 
     5     public static void main(String[] args) {
     6         SpringApplication application = new SpringApplication(Application.class);
     7         // 取消通过命令行设置属性值
     8         application.setAddCommandLineProperties(false);
     9         application.run(Application.class, args);
    10     }
    11 }

    4、开发mapper类连接测试

      user实体类:

    package cn.origal.domain;
    
    public class User {
    
        private String name;
    
        private Integer age;
    
        //  .....  
    }

      mapper类:

     1 package cn.origal.mapper;
     2 
     3 import cn.origal.domain.User;
     4 import org.apache.ibatis.annotations.*;
     5 
     6 import java.util.List;
     7 
     8 public interface UserMapper {
     9 
    10     @Insert("insert into user(name, age) values (#{name}, #{age})")
    11     void insert(User user);
    12 
    13     @Update("update user set age=#{age} where name=#{name}")
    14     void update(User user);
    15 
    16     @Delete("delete from user where name=#{name}")
    17     void delete(User user);
    18 
    19     @Select("select name, age from user")
    20     @Results({
    21             @Result(property = "name",  column = "name"),
    22             @Result(property = "age", column = "age")
    23     })
    24     List<User> getAll();
    25 }

      测试类:

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest(classes = Application.class)
     3 public class TestMybatis {
     4 
     5     @Autowired
     6     private UserMapper userMapper;
     7 
     8     @Test
     9     public void testInsert() {
    10         User user = new User();
    11         user.setName("a2");
    12         user.setAge(11);
    13 
    14         userMapper.insert(user);
    15     }
    16 
    17     @Test
    18     public void query() {
    19         System.out.println(userMapper.getAll());
    20     }
    21 }

    5、通过xml文件连接测试

      (1)首先在上面的那些配置还是需要的,然后在application.properties配置文件中添加xml文件配置:

    # 定义注解类所在的包
    mybatis.config-locations=classpath:mybatis/mybatis-config.xml
    mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

      目录结构:

      (2)在mybatis配置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     <typeAliases>
    6     </typeAliases>
    7 </configuration>

      (3)添加User的映射文件UserMapper.xml:

     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="cn.origal.mapper.UserXmlMapper">
     5 
     6     <select id="getAll" resultType="User">
     7         SELECT
     8         name, age
     9         FROM user
    10     </select>
    11 </mapper>

      (4)编写对应的dao层:

     1 package cn.origal.mapper;
     2 
     3 import cn.origal.domain.User;
     4 
     5 import java.util.List;
     6 
     7 public interface UserXmlMapper {
     8 
     9     List<User> getAll();
    10 }

      (5)进行连接测试:

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest(classes = Application.class)
     3 public class TestXmlMybatis {
     4 
     5     @Autowired
     6     private UserXmlMapper userXmlMapper;
     7 
     8     @Test
     9     public void test() {
    10         System.out.println(userXmlMapper.getAll());
    11     }
    12 }
  • 相关阅读:
    visual sudio开发工具使用小技巧
    JS去除右边的逗号
    下拉标题
    sp_addextendedproperty
    触发器的使用
    缺失一个正数
    组合总和 去重
    拖动 Drag
    n皇后问题
    括号生成
  • 原文地址:https://www.cnblogs.com/origalom/p/8324859.html
Copyright © 2011-2022 走看看