spring boot 集成 mybatis

项目结构



application.propreties


server.port=8520
spring.mvc.static-path-pattern=/static/**
spring.resources.cache-period=864000
spring.mvc.view.prefix=
spring.mvc.view.suffix=vm
spring.http.converters.preferred-json-mapper=fastjson

# datasource
spring.datasource.url=jdbc:mysql://192.168.1.90:8066/db_usercenter?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=webapp
spring.datasource.password=tanbao2017
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#解决中文乱码
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8


mybatis.type-aliases-package=com.evil.mapper
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
#mybatis.config=classpath:mybatis/mybatis-config.xml

logging.level.com.evil=debug

controller 层


package com.evil.controller;

import com.evil.model.User;
import com.evil.model.UserInfo;
import com.evil.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/14.
 */
@Controller
public class userController {
    @Autowired
    HelloService service;


    @RequestMapping("getOneUser")
    @ResponseBody
    public UserInfo getOneUser(){
        UserInfo userInfo = service.get();
        
        return userInfo;
    }


    @RequestMapping("getOneUSerInfo")
    @ResponseBody
    public Map getOneUserInfo(){
        Map result = new HashMap<>();
        UserInfo user = new UserInfo();
        user.setAge((byte)20);
        user.setUserName("5555");
        result.put("data",user);
        return result;
    }

}


service

package com.evil.service;

import com.evil.mapper.UserInfoMapper;
import com.evil.model.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
@Transactional
public class HelloService {
   @Autowired
    private UserInfoMapper userInfoMapper;



    public String hello(){
        return "hello";
    }

    public UserInfo get(){
        return  userInfoMapper.selectByPrimaryKey(2l);
    }
}


userInfoMapper
有红底的

File-Project Structure 页面 Facets下删掉 Spring(直接右键Delete)

这个解答是对的。并不会降低安全性!!因为创建项目的时候,都是先创建空项目再创建web moduele(你想直接创建web project也可以),一般不会使用spring组件。都是自己配置的。这时候你要是不小心手滑(手滑原因:因为idea对你spring的配置文件会在上方报警告,然后你一fix,就容易出事),那就会报错无法Autowired。所以你只要删掉你手滑添加的就可以




 



2

File-Project Structure 页面 Facets下删掉 Spring(直接右键Delete)



2

File-Project Structure 页面 Facets下删掉 Spring(直接右键Delete)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值