springboot mybatis 整合

2021-07-20 06:08

阅读:672

标签:path   驱动   ring   serial   email   catalina   配置   info   jdbc   

新建项目在上一篇.

第二步:创建表和相应的实体类

技术分享图片

实体类:user.java

package com.qtt.im.entity;

import java.io.Serializable;

public class User implements Serializable {
    private Long id;
    private String loginName;
    private String pass;
    private String mobile;
    private String email;
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

第三步:创建写mapper接口,Service,controller

以一个查询为例:

UserMapper.java(此接口用于xml文件和service之间的连接)

package com.qtt.im.mapper;

import com.qtt.im.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {

    public User getUserList();
}

UserService.java

package com.qtt.im.service;

import com.qtt.im.entity.User;

public interface UserService {
    public User getUserList();
}

UserServiceImpl.java(usermapper会报错,但是不影响运行)

package com.qtt.im.service.impl;

import com.qtt.im.entity.User;
import com.qtt.im.mapper.UserMapper;
import com.qtt.im.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper usermapper;
    public User getUserList() {
        return usermapper.getUserList();
    }
}

UserController.java

package com.qtt.im.controller;


import com.qtt.im.entity.User;
import com.qtt.im.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value="/user")
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("/getUserList")
    public User getUserList(){
        User userList = userService.getUserList();
        return userList;
    }

}

所用的注解可以自己网上查询.

第四步:可以添加一些过滤器

MyFilter.java

package com.qtt.im.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        System.out.println("this is my filter url:"+request.getRequestURI());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

WebConfiguration.java

package com.qtt.im.filter;

import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {

    @Bean
    public RemoteIpFilter remoteIpFilter(){
        return  new RemoteIpFilter();
    }
    @Bean
    public FilterRegistrationBean testFilterRegistration(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("paramName","paramValue");
        filterRegistrationBean.setName("MyFilter");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }
}

 

第五步:编写一个程序入口

Application.java

package com.qtt.im;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.qtt.im.mapper")
public class Application {
    public static void  main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

 

 

java代码部分的结构图如下:

技术分享图片

技术分享图片

第六步:创建写sql语句的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="com.qtt.im.mapper.UserMapper" >
    select id="getUserList" resultType="com.qtt.im.entity.User" >
        SELECT id,loginName FROM im_user;
    select>
mapper>

其中 namespace:是java部分的userMapper接口.resultType:是实体类.返回类型.

第七步:创建配置文件

application.proterties

spring.datasource.url=jdbc:mysql://localhost:3306/sdt?useUnicode=true&characterEncoding=utf-8         (sdt是数据库)
mybatis.type-aliases-package=com.qtt.im.mapper                                                 (mapper接口扫描)
spring.datasource.driver-class-name =  com.mysql.jdbc.Driver                                    (驱动)
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root                                                                 (用户名)
spring.datasource.password=qintongtong                                                          (密码)
mybatis.mapper-locations=classpath*:/mappers/**Mapper.xml                                       (xml文件)

log4j.properties

log4j.rootLogger=WARN, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n

配置文件的结构图如下:

技术分享图片

注:userMapper.xml文件所在的目录一定要设成如下所示:file--->Project structtre-->moudles

技术分享图片

如果不设置,就会找不到xml文件,或者可以直接放在resources下,但是不便于管理.

第八步:启动项目测试

技术分享图片

启动这个类后,在浏览器输入地址:成功后如下

技术分享图片

user  和  getUserList是你在controller层定义的

 

springboot mybatis 整合

标签:path   驱动   ring   serial   email   catalina   配置   info   jdbc   

原文地址:https://www.cnblogs.com/qtt1994/p/9519090.html


评论


亲,登录后才可以留言!