<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
dependencies>
spring:
datasource:
driver-class-name : com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm
username: root
password: root
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #springboot集成mybatis 配置Mapper.xml映射文件路径
config-location: classpath:mybatis/config/mybatis-config.xml #springboot集成mybatis 配置mybatis配置文件路径
/**
* @Description:
* @Author: zrblog
* @CreateTime: 2018-09-23 22:32
* @Version:v1.0
*/
@Repository
public interface UserMapper {
User queryUserById(Integer id);
List<User> queryAllUser();
}
UserService:
/**
* @Description:
* @Author: zrblog
* @CreateTime: 2018-09-22 10:52
* @Version:v1.0
*/
public interface UserService {
User queryUserById(Integer id);
List<User> queryAllUser();
}
UserServiceImpl:
/**
* @Description:
* @Author: zrblog
* @CreateTime: 2018-09-22 10:57
* @Version:v1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User queryUserById(Integer id) {
return userMapper.queryUserById(id);
}
@Override
public List<User> queryAllUser() {
return userMapper.queryAllUser();
}
}
/**
* @Description:
* @Author: zrblog
* @CreateTime: 2018-09-22 11:10
* @Version:v1.0
*/
@RestController
@RequestMapping("/")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "user/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<JsonResult> queryUserById(@PathVariable(value = "id") Integer id) {
JsonResult jsonResult = new JsonResult();
try {
User user = userService.queryUserById(id);
jsonResult.setResult(user);
jsonResult.setStatus("success");
} catch (Exception e) {
jsonResult.setResult(e.getMessage());
jsonResult.setStatus("success");
}
return ResponseEntity.ok(jsonResult);
}
@RequestMapping(value = "users", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<JsonResult> queryUserById() {
JsonResult jsonResult = new JsonResult();
try {
List<User> users = userService.queryAllUser();
jsonResult.setResult(users);
jsonResult.setStatus("success");
} catch (Exception e) {
jsonResult.setResult(e.getMessage());
jsonResult.setStatus("success");
}
return ResponseEntity.ok(jsonResult);
}
}
注意:
// xxxMapper接口可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
<configuration>
<typeAliases>
typeAliases>
configuration>
<mapper namespace="com.springboot.cros.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.springboot.cros.domain.User">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR"/>
<result column="age" property="age" jdbcType="INTEGER"/>
<result column="ctm" property="ctm" jdbcType="TIMESTAMP"/>
resultMap>
<sql id="Base_Column_List">
id,username,age,ctm
sql>
<select id="queryUserById" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM tb_user
WHERE id = #{id,jdbcType=INTEGER}
select>
<select id="queryAllUser" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM tb_user
select>
mapper>
注意:
// xxxMapper接口可以使用@Mapper注解,但是每个mapper都加注解比较麻烦,所以统一配置@MapperScan在扫描路径在application类中
@SpringBootApplication
@MapperScan(value = "com.springboot.mybatis.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" style="text:css"/>
<script type="application/javascript" src="js/jquery.js">script>
<script type="application/javascript" src="bootstrap/js/bootstrap.js">script>
<script type="application/javascript" type="text/javascript">
function crosRequest(){
$.ajax({
url:'http://localhost:8080/users',
type:'get',
dataType:'json',
success:function(data){
console.log(data);
}
});
}
script>
head>
<body>
<button type="button" class="btn btn-link" onclick="crosRequest()">请求跨域资源button>
body>
html>
报错,不能访问,存在跨域问题
@CrossOrigin
@RequestMapping(value = "users", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<JsonResult> queryAllUser() {
JsonResult jsonResult = new JsonResult();
try {
List<User> users = userService.queryAllUser();
jsonResult.setResult(users);
jsonResult.setStatus("success");
} catch (Exception e) {
jsonResult.setResult(e.getMessage());
jsonResult.setStatus("success");
}
return ResponseEntity.ok(jsonResult);
}
@Configuration
public class CrosConfig {
@Bean
public WebMvcConfigurer crosConfigurer(){
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
.allowCredentials(false).maxAge(3600);
}
};
}
}
@Configuration
public class CrosConfigFilter {
@Bean
public FilterRegistrationBean crosFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration cros = new CorsConfiguration();
cros.setAllowCredentials(false);
cros.addAllowedOrigin("*");
cros.addAllowedHeader("*");
cros.addAllowedMethod("*");
source.registerCorsConfiguration("/**", cros);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}