新建一个项目
创建好之后,配置一下之前的maven,用默认的要下载好久
配置文件application.yml
从以前的项目copy过来的,一直用这个当模板,改一下数据库名,用户名,密码
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/my12306?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
http:
encoding.force: true
encoding.charset: UTF-8
encoding.enabled: true
mybatis:
type-aliases-package: com.zx.my12306.domain
mapper-locations: classpath:mapper/*.xml
#type-handlers-package: com.zx.his.domain
config-location: classpath:mybatis-config.xml
server:
port: 80
tomcat.uri-encoding: UTF-8
pom.xml 添加jar包和逆向工程插件
注意逆向工程插件的添加位置,一定要在buil标签下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zx</groupId>
<artifactId>my12306</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>My12306</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.4.1</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--逆向工程插件-->
<!--mybatis-generator-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<!--允许移动生成的文件 -->
<verbose>true</verbose>
<!-- 是否覆盖 -->
<overwrite>true</overwrite>
<!-- 自动生成的配置 -->
<configurationFile>
src/main/resources/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<!--mybatis-generator-->
</plugins>
</build>
</project>
MySQL创建数据库
create database my12306;
use my12306;
create table user(
id int primary key auto_increment,
loginname varchar(20) not null,
password varchar(20) not null);
insert into user values(1,'zx','123456');
create table contact(
id int primary key auto_increment,
name varchar(20),
idtype varchar(20),
idcard varchar(20),
type varchar(20),
phone varchar(20),
userid int not null,
foreign key(userid) references user(id));
insert into contact values(2,'哎米米','身份证','31010119204163983','学生','13912341234',1);
insert into contact values(1,'陈伟飞','身份证','37010519880414805X','成人','13912341200',1);
配置数据库
逆向工程配置文件 mybatis-generator.xml(文件名不可修改)
在src/main/resources下添加一个mybatis-generator.xml文件
<?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>
<context id="myBatis3" targetRuntime="MyBatis3">
<!-- mergeable 为true时,可合并,为false,重新生成的时采用覆盖-->
<property name="mergeable" value="false"/>
<property name="javaFileEncoding" value="UTF-8"/>
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/my12306" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--生成Model类存放位置-->
<javaModelGenerator targetPackage="com.zx.my12306.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--生成映射文件存放位置-->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.zx.my12306.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<table tableName="user" domainObjectName="User" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="id" javaType="Integer"/></table>
<table tableName="contact" domainObjectName="Contact" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false">
<columnOverride column="id" javaType="Integer"/></table>
</context>
</generatorConfiguration>
运行插件,双击生成代码
编写代码,实现登录
UserMapper.java
在自动生成的类中加入一个方和注解
UserMapper.xml
方法名和接口中一致
大括号中参数为user类的属性名
UserSerice.java
自己创建,注意添加注解
UserController.java
自己创建,注意添加注解
结果展示
编写代码,实现按用户id查询联系人
ContactMapper.java
package com.zx.my12306.dao;
import com.zx.my12306.domain.Contact;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface ContactMapper {
int deleteByPrimaryKey(Integer id);
int insert(Contact record);
int insertSelective(Contact record);
Contact selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Contact record);
int updateByPrimaryKey(Contact record);
//新加的方法 按照用户名查询
List<Contact> selectByUserId(int id);
}
ContactMapper.xml
<select id="selectByUserId" parameterType="int" resultType="contact">
select * from contact where userid=#{id}
</select>
ContactService.java
package com.zx.my12306.service;
import com.zx.my12306.dao.ContactMapper;
import com.zx.my12306.domain.Contact;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service
@Transactional
public class ContactService {
@Resource
ContactMapper contactMapper;
public List<Contact> getByUserId(int userId){
return contactMapper.selectByUserId(userId);
}
}
ContactController.java
package com.zx.my12306.controller;
import com.zx.my12306.domain.Contact;
import com.zx.my12306.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ContactController {
@Autowired
ContactService contactService;
@RequestMapping("/getcontactByuserid")
public List<Contact> getByUserId(int userId){
return contactService.getByUserId(userId);
}
}
效果
版权声明:本文为HRBU_17042132原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。