报错
intellij web生成的项目结构
UserController
package com.example.demo10.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.spring.mybatis.springMybatis.dao.UserMapper;
import com.spring.mybatis.springMybatis.entity.User;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserMapper userMapper;
@RequestMapping(value={"/selectUserById"}, method=RequestMethod.GET)
public User selectUserById(String id){
User user = userMapper.selectUserById(Integer.parseInt(id));
return user;
}
@RequestMapping(value={"/selectUserByName"}, method=RequestMethod.GET)
public List<User> selectUserByName(String userName){
return userMapper.selectUserByName(userName);
}
@RequestMapping(value={"/addUser"}, method=RequestMethod.POST)
public void addUser(User user){
userMapper.addUser(user);
}
@RequestMapping(value={"/updateUser"}, method=RequestMethod.POST)
public void updateUser(User user){
userMapper.updateUser(user);
}
@RequestMapping(value={"/deleteUser"}, method=RequestMethod.POST)
public void deleteUser(String id){
userMapper.deleteUser(Integer.parseInt(id));
}
}
UserMapper
package com.example.demo10.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.spring.mybatis.springMybatis.entity.User;
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User selectUserById(int id);
@Select("select * from user where userName = #{userName}")
public List<User> selectUserByName(String userName);
@Insert("insert into user(userName,userAge,userAddress) values (#{userName},#{userAge},#{userAddress})")
public void addUser(User user);
@Update("update user set userName=#{userName},userAge=#{userAge},userAddress=#{userAddress} where id=#{id}")
public void updateUser(User user);
@Delete("delete from user where id=#{id}")
public void deleteUser(int id);
}
User
package com.example.demo10.entity;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class User {
private int id;
private String userName;
private int userAge;
private String userAddress;
}
jdbc
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
pom.xml
<?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.spring.mybatis</groupId>
<artifactId>springMybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springMybatis</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<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.21</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
sql
后面修改
查找所有用户
dao.UserMapper
@Select("select * from user")
public User selectUserAll();
controller.UserController
@RequestMapping(value={"/selectUserAll"}, method=RequestMethod.GET)
public User selectUserAll(){
User user = userMapper.selectUserAll();
return user;
}
跨域
Demo10Application
package com.example.demo10;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
@SpringBootApplication
public class Demo10Application extends WebMvcAutoConfiguration {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowCredentials(true)
.allowedHeaders("*")
.allowedOrigins("*")
.allowedMethods("*");
}
public static void main(String[] args) {
SpringApplication.run(Demo10Application.class, args);
}
}