springboot 简单入门(二)
程序员文章站
2022-05-03 12:52:33
...
1、建表
CREATE TABLE `enjoy_user` (
`id` int NOT NULL AUTO_INCREMENT ,
`passwd` varchar(255) NULL ,
`username` varchar(255) NULL ,
PRIMARY KEY (`id`)
);
2、搭建springboot环境
1)配置xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 新建App.java
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
3、新建Controller
package cn.enjoy.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@RequestMapping("/hello")
public Object sayHello() {
return "hello";
}
}
测试:
运行App,在浏览器输入:localhost:8080/hello,发现“hello”说明第一步部署成功。
4、集成mybatis
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
5、生成mapper
1)在resources目录,新建application.properties文件,增加内容如下:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root1234%
mybatis.mapperLocations=classpath:mapping/*.xml
2)准备mybatis的生成文件generatorConfig.xml,并在相应目录创建好model,dao,mapping文件夹
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="C:\Users\VULCAN\.m2\repository\mysql\mysql-connector-java\5.1.37\mysql-connector-java-5.1.37.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/spring" userId="root" password="root1234%">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="cn.enjoy.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.enjoy.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="enjoy_user" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
3)右键生成:结构图如下
注意:得再App启动类上增加@MapperScan扫描注解
@SpringBootApplication
@MapperScan("cn.enjoy.dao")
public class App {
public static void main(String[] args) throws Exception {
SpringApplication.run(App.class, args);
}
}
6、新增mapper登陆方法
Users findByUsernameAndPasswd(@Param(“username”) String username, @Param(“passwd”) String passwd);
对应的Mapper.XML配置
<select id="findByUsernameAndPasswd" resultType="cn.enjoy.model.Users" parameterType="map">
select
<include refid="Base_Column_List" />
from enjoy_user where 1=1
<if test="passwd != null" >
and passwd = #{passwd,jdbcType=VARCHAR}
</if>
<if test="username != null" >
and username = #{username,jdbcType=VARCHAR}
</if>
limit 1
</select>
7、SpringBoot单元测试
要测试刚才新增的Mapper方法是否成功,这里需要单元测试,在springboot中是有专门的组件来做单元测试的,在pom文件中新增依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
- 新建UserTest放置在test目录下
package cn.enjoy.test;
import cn.enjoy.App;
import cn.enjoy.dao.UsersMapper;
import cn.enjoy.model.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest(classes = {App.class})
@RunWith(SpringRunner.class)
public class UserTest {
@Resource
private UsersMapper usersMapper;
@Test
public void testAdd() {
Users user = new Users() ;
user.setPasswd("123");
user.setUsername("enjoy");
usersMapper.insertSelective(user);
}
@Test
public void testFindUser() {
Users enjoy = usersMapper.findByUsernameAndPasswd("enjoy", "123");
System.out.println(enjoy);
}
}
这样SpringBoot集成单元测试成功!
8、新建service
1)新增接口IUserService
public interface IUserService {
boolean login(String username,String passwd);
boolean register(String username,String passwd);
}
2) 创建实现类
package cn.enjoy.service.impl;
import cn.enjoy.dao.UsersMapper;
import cn.enjoy.model.Users;
import cn.enjoy.service.IUserService;
import javax.annotation.Resource;
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UsersMapper usersMapper;
@Override
public boolean login(String username, String passwd) {
Users users = usersMapper.findByUsernameAndPasswd(username, passwd);
return users != null;
}
@Override
public boolean register(String username, String passwd) {
Users users = new Users();
users.setUsername(username);
users.setPasswd(passwd);
int cnt = usersMapper.insertSelective(users);
return cnt > 0;
}
}
9、修改controller
修改UserController,增加login(登录)和register(注册)方法
package cn.enjoy.controller;
import cn.enjoy.service.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserController {
@Resource
private IUserService iUserService;
@RequestMapping("/hello")
public Object sayHello() {
return "hello";
}
@RequestMapping("/login")
public String login(String username,String passwd) {
boolean login = iUserService.login(username, passwd);
if(login) {
return "登陆成功";
}else {
return "登陆失败";
}
}
@RequestMapping("/register")
public String register(String username,String passwd) {
boolean login = iUserService.register(username, passwd);
if(login) {
return "注册成功";
}else {
return "注册失败";
}
}
}
测试结果:
在浏览器上输入:localhost:8080/register?username=deer&passwd=123,显示“注册成功”
在浏览器上输入:localhost:8080/login?username=deer&passwd=123,显示“登陆成功”
上一篇: JS nodeType返回类型