项目:
—project
—core
—mgrsite
—website
创建maven项目,选用quickstart
project
pom.xml
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.wolfcode.p2p</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<modules>
<module>core</module>
<module>website</module>
<module>mgrsite</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.6</version>
</dependency>
</dependencies>
</project>
core pom.xml
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>cn.wolfcode.p2p</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<name>core</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>false</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
mgrsite pom.xml
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>cn.wolfcode.p2p</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mgrsite</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>cn.wolfcode.p2p</groupId>
<artifactId>core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
website pom.xml
<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent</artifactId>
<groupId>cn.wolfcode.p2p</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>website</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>cn.wolfcode.p2p</groupId>
<artifactId>core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
这样一个p2p项目的骨架就完成了,接下来实现一个注册的功能
在core下编写domain,mapper,service
domain
package cn.wolfcode.p2p.domain;
import java.io.Serializable;
/**
* Create by CrazyCat
*/
public class BaseDomain implements Serializable {
private Long id;
}
package cn.wolfcode.p2p.domain;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class LoginInfo extends BaseDomain{
public static final int STATE_NORMAL=0;//正常情况
public static final int STATE_LOCK=1;//锁定
private String username;
private String password;
private int state;
}
mapper
package cn.wolfcode.p2p.mapper;
import cn.wolfcode.p2p.domain.LoginInfo;
import java.util.List;
public interface LoginInfoMapper {
int insert(LoginInfo record);
LoginInfo selectByPrimaryKey(Long id);
int updateByPrimaryKey(LoginInfo record);
int checkRegister(String username);
}
mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.wolfcode.p2p.mapper.LoginInfoMapper" >
<resultMap id="BaseResultMap" type="cn.wolfcode.p2p.domain.LoginInfo" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="state" property="state" jdbcType="TINYINT" />
</resultMap>
<insert id="insert" parameterType="cn.wolfcode.p2p.domain.LoginInfo" useGeneratedKeys="true" keyProperty="id" >
insert into logininfo (username, password, state
)
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{state,jdbcType=TINYINT}
)
</insert>
<update id="updateByPrimaryKey" parameterType="cn.wolfcode.p2p.domain.LoginInfo" >
update logininfo
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
state = #{state,jdbcType=TINYINT}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select id, username, password, state
from logininfo
where id = #{id,jdbcType=BIGINT}
</select>
<select id="checkRegister" resultType="java.lang.Integer">
select count(id)
from logininfo
where username=#{username}
</select>
</mapper>
service
package cn.wolfcode.p2p.service;
import cn.wolfcode.p2p.domain.LoginInfo;
/**
* Created by CrazyCat on 2018/3/8.
*/
public interface ILoginInfoService {
int insert(LoginInfo record);
LoginInfo selectByPrimaryKey(Long id);
int updateByPrimaryKey(LoginInfo record);
void checkResgister(String username,String password);
}
package cn.wolfcode.p2p.service.impl;
import cn.wolfcode.p2p.domain.LoginInfo;
import cn.wolfcode.p2p.mapper.LoginInfoMapper;
import cn.wolfcode.p2p.service.ILoginInfoService;
import cn.wolfcode.p2p.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by CrazyCat on 2018/3/8.
*/
@Service
@Transactional
public class LoginInfoServiceImpl implements ILoginInfoService {
@Autowired
private LoginInfoMapper loginInfoMapper;
@Override
public int insert(LoginInfo record) {
return loginInfoMapper.insert(record);
}
@Override
public LoginInfo selectByPrimaryKey(Long id) {
return loginInfoMapper.selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKey(LoginInfo record) {
return loginInfoMapper.updateByPrimaryKey(record);
}
@Override
public void checkResgister(String username,String password) {
int count = loginInfoMapper.checkRegister(username);
if(count>0){
throw new RuntimeException("该用户已注册,请重新输入");
}
LoginInfo entity = new LoginInfo();
entity.setUsername(username);
entity.setPassword(MD5.encoder(password));
loginInfoMapper.insert(entity);
}
}
utils
package cn.wolfcode.p2p.utils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* @author Rz
* @date 26/02/2018 21:41
*/
@Getter
@AllArgsConstructor @NoArgsConstructor
public class JSONResult {
private boolean success = true;
private String msg;
public JSONResult setMsg(boolean success, String msg) {
this.success = success;
this.msg = msg;
return this;
}
}
Config.class
package cn.wolfcode.p2p;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
/**
* Created by CrazyCat on 2018/3/8.
*/
@Configuration
@MapperScan("cn.wolfcode.p2p.mapper")
@PropertySource("application-core.properties")
@EnableTransactionManagement
public class CoreConfig {
public static void main(String[] args) {
SpringApplication.run(CoreConfig.class,args);
}
}
application-core.properties
spring.datasource.druid.username=root
spring.datasource.druid.password=admin
spring.datasource.druid.url=jdbc:mysql:///p2p
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
website部分
controller
package cn.wolfcode.p2p.controller;
import cn.wolfcode.p2p.service.ILoginInfoService;
import cn.wolfcode.p2p.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by CrazyCat on 2018/3/8.
*/
@Controller
public class RegisterController {
@Autowired
private ILoginInfoService loginInfoService;
@RequestMapping("checkRegister")
@ResponseBody
public Object checkRegister(String username,String password){
JSONResult result = new JSONResult();
try{
loginInfoService.checkResgister(username,password);
result.setMsg(true,"注册成功");
}catch(Exception e){
result.setMsg(false,e.getMessage());
}
return result;
}
}
config.class
package cn.wolfcode.p2p;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* Created by CrazyCat on 2018/3/8.
*/
@SpringBootApplication
public class WebConfig {
public static void main(String[] args) {
SpringApplication.run(WebConfig.class,args);
}
}
也需要一个application.properties资源文件,可以不配置参数,也可以在这里修改端口
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/checkRegister" method="post">
<input type="text" name="username"/><br/>
<input type="text" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
如何数据库没有这条数据,效果如下:
版权声明:本文为qq_33551131原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。