Loading

03-SpringMvc整合Mybatis

0、搭建SpringMVC框架

参考文章:IDEA搭建SpringMVC项目

1、添加依赖

在上一篇文章的基础上添加如下依赖

<!-- mybatis核心包 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

<!-- mybatis/spring包 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.25</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.1</version>
</dependency>

2、整合Mybatis

  1. 数据库连接数据

    db.driverClassName=com.mysql.cj.jdbc.Driver
    db.url=jdbc:mysql://127.0.0.1:3306/xxx?serverTimezone=GMT%2B8
    db.username=root
    db.password=xxx
    
  2. Spring整合Mybatis

    根应用上下文spring-application.xml文件添加mybatis配置

    <!-- 0、加载数据库连接数据 -->
    <context:property-placeholder location="classpath:db.properties"/>
    
    <!-- 1、配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    
    <!-- 2、配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--1. 数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--2. 别名-->
        <property name="typeAliasesPackage" value="org.omaster.bean"/>
        <!--3. sql映射文件 -->
        <property name="mapperLocations" value="classpath:omaster/mapper/*.xml"/>
    </bean>
    
    <!-- 3、配置映射器接口 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.omaster.mapper"/>
    </bean>
    

3、编写代码

  1. sql映射接口

    org.omaster.mapper.TimeMapper

    public interface TimeMapper {
    
        List<User> findAll();
    }
    
  2. sql映射文件

    resource目录下新建目录:omaster/mapper,在该目录下创建文件timeMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="org.omaster.mapper.TimeMapper">
    
        <select id="findAll" resultType="org.omaster.bean.User">
            select id, name, time_one timeOne from time_zone
        </select>
    </mapper>
    
  3. 服务层接口

    org.omaster.mapper.TimeService

    @Service
    public class TimeService {
    
        @Autowired
        private TimeMapper timeMapper;
    
        public List<User> findAll() {
            return timeMapper.findAll();
        }
    }
    
  4. 控制层接口

    实体对象

    public class User {
        private Integer id;
        private String name;
    
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT")
        private Date timeOne;
    
    	// get/set、toString方法等
    }
    

    org.omaster.controller.TimeController

    @Controller
    public class TimeController {
    
        @Autowired
        private TimeService timeService;
    
        @GetMapping("/getAll")
        @ResponseBody
        public List<User> findAll() {
            List<User> all = timeService.findAll();
            System.out.println(all);
            return all;
        }
    }
    

3、测试

启动项目,访问http://localhost:8080/SpringProject/getAllimage-20220227194737638

posted @ 2022-02-27 19:50  OMaster  阅读(19)  评论(0编辑  收藏  举报