超级无敌狂爆龙战士
在這里插入代碼片
`###POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><packaging>war</packaging><name>springboot_build</name><groupId>com.jishi</groupId><artifactId>springboot_build</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId></exclude></excludes></configuration></plugin></plugins></build><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.5.5</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.5.5</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>2.5.5</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.41</version></dependency><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency></dependencies></project>###```java
在這里插入代碼片
###bean包下
的User類
package com.jishi.boot.bean;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.stereotype.Component;@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
@Component
public class User {private Integer id;private String username;private String password;
}###controler下的UserController
package com.jishi.boot.controller;import com.jishi.boot.bean.User;
import com.jishi.boot.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;@RequestMapping("/user")
@Controller
public class UserController {@ResourceUserService userService;@ResponseBody@RequestMapping("/login")public String hello(User user){if (userService.login(user) != null){System.out.println("打印一下該查詢的值"+userService.login(user));return "登入成功";}else {return "登入失敗";}}
}`
###service包下的接口UserService
package com.jishi.boot.service;import com.jishi.boot.bean.User;
import org.springframework.stereotype.Service;@Service
public interface UserService {public Integer login (User user);
}###service包下的UserServiceImp
package com.jishi.boot.service.Imp;import com.jishi.boot.bean.User;
import com.jishi.boot.dao.UserDao;
import com.jishi.boot.service.UserService;
import org.springframework.stereotype.Service;import javax.annotation.Resource;@Service
public class UserServiceImp implements UserService {@ResourceUserDao userDao;@Overridepublic Integer login(User user) {return userDao.login(user);}
}###dao包下的UserDao
package com.jishi.boot.dao;import com.jishi.boot.bean.User;
import org.apache.ibatis.annotations.Mapper;public interface UserDao {/*** 登入* @param user* @return*/public Integer login (User user);
}###mapper包下的userDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.jishi.boot.dao.UserDao"><select id="login" resultType="java.lang.Integer" >SELECT *FROM loginuserWHERE id = #{id} AND username = #{username} AND password = #{password}</select>
</mapper>###resources包下的application.yaml
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/curd?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=falsedriver-class-name: com.mysql.jdbc.Drivermvc:hiddenmethod:filter:enabled: truemybatis:configuration:map-underscore-to-camel-case: truemapper-locations: classpath:mapper/*.xml###與各層級同層的啟動類
package com.jishi.boot;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@MapperScan("com.jishi.boot.dao")
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {SpringApplication.run(MainApplication.class, args);}
}###crud的mapperDao。簡單sql直接寫在dao上
package com.example.crud.mapper;import com.example.crud.pojo.User;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserMapper {/*** 查詢?nèi)繑?shù)據(jù)*/@Select("select * from user")public List<User> findAll();*** 新增數(shù)據(jù)*/@Insert("insert into user (username, password) values (#{username}, #{password})")public int save(User user);/*** 刪除數(shù)據(jù)*/@Delete("delete from user where id=#{id}")public int delete(int id);/*** 根據(jù)ID查找用戶*/@Select("select * from user where id=#{id}")public User get(int id);/*** 根據(jù)ID更新用戶數(shù)據(jù)*/@Update("update user set username=#{username},password=#{password} where id=#{id}")public int updateById(User user);
}
總結(jié)
- 上一篇: 【无标题】c语言指针2333
- 下一篇: 数据结构(二): 链表篇