51,397
社区成员




package com.demo.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.demo.model.User;
@Mapper
public interface UserDao {
public List<User> selectUser(int id);
}
package com.demo.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.demo.dao.UserDao;
import com.demo.model.User;
@Service
public class UserService {
@Resource
private UserDao userdao;
public List<User> userSelectService(int id) throws Exception{
System.out.println("测试1");
List<User> list = userdao.selectUser(id);
System.out.println(list);
return list;
}
}
package com.demo.controller;
import java.util.ArrayList;
import java.util.List;
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;
import com.demo.model.User;
import com.demo.service.UserService;
@Controller
@RequestMapping("/")
public class SpringBootUserController {
@Autowired
private UserService userService;
/**
* 测试查询
* @param id
* @return
*/
@RequestMapping("/userSelectAction")
@ResponseBody
public List<User> userSelectAction(int id) throws Exception{
System.out.println("测试2");
List<User> list= userService.userSelectService(id);
System.out.println(list);
return list;
}
/**
* 只是测试
* @return
*/
@RequestMapping(value="/hello")
@ResponseBody
public String hello() {
return "测试";
}
}
package com.demo;
import org.mybatis.spring.annotation.MapperScan;
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.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication // Spring Boot项目的核心注解,主要目的是开启自动配置
@ComponentScan(basePackages = {"com.demo"})
@MapperScan(value="com.demo")
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class TestController {
// 在main方法中启动一个应用,即:这个应用的入口
public static void main(String[] args) {
SpringApplication.run(TestController.class, args);
}
}
<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.demo</groupId>
<artifactId>Mustang</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 1、设置Spring boot的parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<properties>
<!-- 声明项目配置依赖编码格式为 utf-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- java jdk 版本声明 可变更 根据自己配置去匹配 -->
<java.version>1.8</java.version>
<fastjson.version>1.2.24</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot核心,包括自动配置支持,日志和YAML -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- DevTools in Spring Boot 项目热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 数据库配置 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<!-- 配置mapper.xml文件路劲 -->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。-->
<!-- 如果使用maven的spring-boot:run的话就不需要此配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#datasource
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.name=demo
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# FREEMARKER (FreeMarkerAutoConfiguration)
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
mybatis.mapper-locations=Classpath:mapper/*.xml
mybatis.type-aliases-package=com.demo.dao
#mybatis.typeAliasesPackage: com.demo.dao.UserDao
#mybatis.mapperLocations=Classpath:mapper/*.xml
#server
server.port=8080
spring.session.store-type=none
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'springBootUserController': Unsatisfied dependency expressed through field 'userService';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService':
Injection of resource dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao'
defined in file [F:\Work\Mustang\target\classes\com\demo\dao\UserDao.class]: Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException:
Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
这个User的逻辑有问题