坑一:pom文件依赖标红
解决:在前面的dependency依赖里加进去
坑二:
运行时出错:Failed to determine a suitable driver class
原因:resources文件夹命名错误,必须为resources。修改之后重新打包——run——edit configuration
Run——run——选择如下图所示选项
结果生成:
回顾第一步:
新建工程maven——create from archi——
目录结构:
1、mybatis-generator.xml要在resources下,不能放在mapping包里,否则启动会报空指针。
2、为什么在配置文件里配置路径:
原因:
如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。
如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。
此时spring是通过识别mapper.xml中的 <mapper namespace="com.fan.mapper.UserDao"> namespace的值来确定对应的Mapper.class的。
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.miaosha.project</groupId>
<artifactId>miaosha</artifactId>
<version>1.0-SNAPSHOT</version>
<name>miaosha</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</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>
</plugins>
</pluginManagement>
</build>
</project>
package com.miaosha.project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* Hello world!
*
*/
@EnableAutoConfiguration
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class,args);
}
}
server.port=8090
mybatis.mapper-locations=classpath:mapping/*.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="DB2Tables" targetRuntime="MyBatis3">
<!--数据库连接地址和密码-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/miaosha"
userId="root"
password="root">
</jdbcConnection>
<!--存放DataObject类的地址 -->
<javaModelGenerator targetPackage="com.miaosha.project.dataObject"
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="com.miaosha.project.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<table tableName="user_info" domainObjectName="UserDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
<table tableName="user_password" domainObjectName="UserPasswordDO" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
删除example的只剩下如图:
第二步:
1、配置文件新增:
ing.datasource.name=miaosha
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
#使用druid数据源
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2、修改APP.class为如图
SpringApplication.run(App.class,args);
package com.miaosha.project;
import com.miaosha.project.dao.UserDOMapper;
import com.miaosha.project.dataObject.UserDO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@SpringBootApplication
@RestController
@MapperScan("com.miaosha.project.dao")
public class App {
@Autowired
private UserDOMapper userDOMapper;
@RequestMapping("/1")
public String home(){
UserDO userDO = userDOMapper.selectByPrimaryKey(1);
if(userDO == null){
return "用户信息不存在";
}else{
return userDO.getName();
}
}
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class,args);
}
}
坑三:一开始访问不到:404
应该是没有扫描到,可能和左侧目录结构有关
丢掉了RestController注解
三、
新增
package com.miaosha.project.service;
import com.miaosha.project.service.model.UserModel;
public interface UserService {
UserModel getUserById(Integer id);
}
package com.miaosha.project.service.impl;
import com.miaosha.project.dao.UserDOMapper;
import com.miaosha.project.dao.UserPasswordDOMapper;
import com.miaosha.project.dataObject.UserDO;
import com.miaosha.project.dataObject.UserPasswordDO;
import com.miaosha.project.service.UserService;
import com.miaosha.project.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDOMapper userDOMapper;
@Autowired
private UserPasswordDOMapper userPasswordDOMapper;
@Override
public UserModel getUserById(Integer id) {
UserDO userDO = userDOMapper.selectByPrimaryKey(id);
if(userDO == null){
return null;
}
//通过用户id获取用户加密信息
UserPasswordDO userPasswordDO = userPasswordDOMapper.selectByUserId(userDO.getId());
return convertFromDataObject(userDO,userPasswordDO);
}
private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO){
if(userDO == null){
return null;
}
UserModel userModel = new UserModel();
BeanUtils.copyProperties(userDO,userModel);
if(userPasswordDO != null){
userModel.setEncrptPassword(userPasswordDO.getEncrptPassword());
}
return userModel;
}
}
package com.miaosha.project.service.model;
public class UserModel {
private Integer id;
private String name;
private Byte gender;
private Integer age;
private String telphone;
private String risterMode;
private String thirdPartyId;
private String encrptPassword;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getGender() {
return gender;
}
public void setGender(Byte gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
public String getRisterMode() {
return risterMode;
}
public void setRisterMode(String risterMode) {
this.risterMode = risterMode;
}
public String getThirdPartyId() {
return thirdPartyId;
}
public void setThirdPartyId(String thirdPartyId) {
this.thirdPartyId = thirdPartyId;
}
public String getEncrptPassword() {
return encrptPassword;
}
public void setEncrptPassword(String encrptPassword) {
this.encrptPassword = encrptPassword;
}
}
新增内容UserPasswordDOMapper.xml:
<sql id="Base_Column_List">
id,encrpt_password,user_id
</sql>
<select id="selectByUserId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_password
where user_id = #{userId,jdbcType=INTEGER}
</select>
package com.miaosha.project.controller;
import com.miaosha.project.service.UserService;
import com.miaosha.project.service.model.UserModel;
import com.sun.org.apache.xpath.internal.operations.Mod;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller("user")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get")
@ResponseBody
public UserModel getUser(@RequestParam("id") Integer id){
UserModel userModel = userService.getUserById(id);
return userModel;
}
}
package com.miaosha.project.dao;
import com.miaosha.project.dataObject.UserPasswordDO;
public interface UserPasswordDOMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_password
*
* @mbg.generated Tue Jan 08 17:11:48 CST 2019
*/
int insert(UserPasswordDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user_password
*
* @mbg.generated Tue Jan 08 17:11:48 CST 2019
*/
int insertSelective(UserPasswordDO record);
UserPasswordDO selectByUserId(Integer userId);
}
访问:
新增:返回前端的对象
package com.miaosha.project.controller.viewObject;
public class UserVo {
private Integer id;
private String name;
private Byte gender;
private Integer age;
private String telphone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getGender() {
return gender;
}
public void setGender(Byte gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
package com.miaosha.project.controller;
import com.miaosha.project.controller.viewObject.UserVo;
import com.miaosha.project.service.UserService;
import com.miaosha.project.service.model.UserModel;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.BeanUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller("user")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/get")
@ResponseBody
public UserVo getUser(@RequestParam("id") Integer id){
UserModel userModel = userService.getUserById(id);
return convertFromUserModel(userModel);
}
private UserVo convertFromUserModel(UserModel userModel){
if(userModel == null){
return null;
}
UserVo userVo = new UserVo();
//字段名以及类型均一致
BeanUtils.copyProperties(userModel,userVo);
return userVo;
}
}
访问:
报错:降低安全级别:https://blog.youkuaiyun.com/wangmaohong0717/article/details/80352349