JAVAEE-创建最基本的spring-boot项目

2019-06-26  本文已影响0人  XINHAO_HAN

创建项目<Spring-boot>

Spring-Initializr

image.png image.png image.png

设置maven

image.png

设置包

image.png

需要加入的注解(sql操作层)

@Component
@Select("SELECT id, username, password FROM wxapp WHERE USERNAME = #{username} ")

全部代码

package com.xinhao.conterller.testapp.mapper

import com.xinhao.conterller.testapp.bean.UserDataBean
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.springframework.stereotype.Component

@Component
interface LoginMapper {


    /**
     *
     * 登陆
     *
     */
    @Select("SELECT id, username, password FROM wxapp WHERE USERNAME = #{username} ")
    fun login(@Param("usernam") username: String): UserDataBean

数据bean[需要加入的注解]

@Data

全部代码

package com.xinhao.conterller.testapp.bean

import lombok.Data

@Data
data class UserDataBean(val id: Int, val username: String, val password: String)

Dao层需要加入的注解

@Component
 @Autowired

全部代码

package com.xinhao.conterller.testapp.dao.impl

import com.xinhao.conterller.testapp.bean.UserDataBean
import com.xinhao.conterller.testapp.dao.inter.ILoginDao
import com.xinhao.conterller.testapp.mapper.LoginMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class LoginDao : ILoginDao {

    @Autowired
    lateinit var mLoginMapper: LoginMapper

    override fun loginUser(username: String): UserDataBean {


        return mLoginMapper.login(username)

    }
}

service层全部注解

@Service
@Autowired

全部代码

package com.xinhao.conterller.testapp.service.impl

import com.xinhao.conterller.testapp.bean.UserDataBean
import com.xinhao.conterller.testapp.dao.inter.ILoginDao
import com.xinhao.conterller.testapp.service.inter.ILoginService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service


@Service
class LoginServiceImpl : ILoginService {

    @Autowired
    lateinit var mILoginDao: ILoginDao

    override fun loginUser(username: String, password: String): UserDataBean {


        return mILoginDao.loginUser(username)
    }
}

连接层全部注解

@RestController
@RequestMapping("xinhao")
@GetMapping("login")
@Autowired
@PostMapping("login")

全部代码

package com.xinhao.conterller.testapp.conterller

import com.xinhao.conterller.testapp.service.inter.ILoginService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("xinhao")
class LoginConterller {


    @Autowired
    lateinit var mIoginService: ILoginService

    @GetMapping("login")
    fun login(@RequestParam("username") username: String, @RequestParam("password") password: String): String? {


        return mIoginService.loginUser(username, password).password
    }

}

默认自带配置

@SpringBootApplication(scanBasePackages = {"你的包名.**"})
@MapperScan(basePackages = {"你的mapper包【全称路径包名】.**"})
//示例
@SpringBootApplication(scanBasePackages = {"com.xinhao.**"})
@MapperScan(basePackages = {"com.xinhao.conterller.testapp.mapper.**"})

配置sql链接信息[application.yml]

server:
  port: 8080
spring:
  datasource:
    username: root
    password: 123
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.1.197:3306/xinhaotest?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC

图片

image.png

完成!

-----------------------------------------------------------创建最基本的【无需sql语句】

在Application.yml中再加入


mybatis:
  configuration:
    map-underscore-to-camel-case: true

mapper:
  mappers:
    - tk.mybatis.mapper.common.Mapper
    - tk.mybatis.mapper.common.BaseMapper
    - tk.mybatis.mapper.common.IdsMapper
    - tk.mybatis.mapper.common.ConditionMapper
    - tk.mybatis.mapper.common.ExampleMapper
  notEmpty: true

创建一个核心Mapper具体这样写

import tk.mybatis.mapper.common.BaseMapper
import tk.mybatis.mapper.common.ConditionMapper
import tk.mybatis.mapper.common.IdsMapper
import tk.mybatis.mapper.common.special.InsertListMapper


/**
 *
 * 核心Mapper
 *
 */
@Mapper
interface CoreMapper<T> : BaseMapper<T>, ConditionMapper<T>, IdsMapper<T>, InsertListMapper<T>

创建一个集成于核心类的子类,并写上你javaBean的泛型

package com.xinhao.testweb.web.mapper.login

import com.xinhao.testweb.web.bean.XinhaoBean
import com.xinhao.testweb.web.mapper.CoreMapper
@Mapper
interface LoginMapper : CoreMapper<XinhaoBean>

然后在service层

package com.xinhao.testweb.web.service.impl

import com.xinhao.testweb.web.mapper.login.LoginMapper
import com.xinhao.testweb.web.service.inter.ILoginService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service

class LoginServiceImpl : ILoginService {
    
    @Autowired
    lateinit var mLoginMapper:LoginMapper
    
    override fun loginUser(username: String, password: String): String {


        
        
        
        

        return null!!
    }
}

就可以使用了

上一篇下一篇

猜你喜欢

热点阅读