java ee spring boot_JAVAEE-创建最基本的spring-boot项目

创建项目

Spring-Initializr

72fa147bf3ba

image.png

72fa147bf3ba

image.png

72fa147bf3ba

image.png

设置maven

72fa147bf3ba

image.png

设置包

72fa147bf3ba

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

图片

72fa147bf3ba

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 : BaseMapper, ConditionMapper, IdsMapper, InsertListMapper

创建一个集成于核心类的子类,并写上你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

然后在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!!

}

}

就可以使用了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值