整合流程:java
1、項目結構mysql
2、上代碼web
App.javaspring
1 package com.CCBK.app; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * Hello world! 8 * 9 */ 10 //@EnableAutoConfiguration 11 //@ComponentScan 12 13 //同時使用上面兩個註解或者單獨使用下面一個註解纔能有掃描功能,掃描其餘的controller 14 @SpringBootApplication 15 public class App { 16 17 public static void main(String[] args) throws Exception { 18 SpringApplication.run(App.class, args); 19 } 20 }
UserController.javasql
package com.CCBK.app.controller; 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.CCBK.app.entity.User; import com.CCBK.app.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/getUserInfo") @ResponseBody public List<User> getUserInfo() { List<User> user = userService.getUserInfo(); System.out.println(user.toString()); return user; } @RequestMapping("/addUserInfo") @ResponseBody public String addUserInfo() { User user = new User(); user.setId(3L); user.setName("cwh"); userService.insert(user); return "success:" + user.toString(); } }
User.java數據庫
package com.CCBK.app.entity; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (id != null ? !id.equals(user.id) : user.id != null) return false; return true; } @Override public int hashCode() { return id != null ? id.hashCode() : 0; } }
UserMapper.javaapache
package com.CCBK.app.mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.CCBK.app.entity.User; @Mapper public interface UserMapper { public List<User> findUserInfo(); public int addUserInfo(User user); public int delUserInfo(int id); }
UserService.javatomcat
package com.CCBK.app.service; import java.util.List; import com.CCBK.app.entity.User; public interface UserService { public List<User> getUserInfo(); public void insert(User user); }
UserServiceImpl.javaspringboot
package com.CCBK.app.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.CCBK.app.entity.User; import com.CCBK.app.mapper.UserMapper; import com.CCBK.app.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public List<User> getUserInfo() { return userMapper.findUserInfo(); } public void insert(User user) { userMapper.addUserInfo(user); } }
UserMapper.xmlmybatis
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.CCBK.app.mapper.UserMapper"> <select id="findUserInfo" resultType="com.CCBK.app.entity.User"> select id,name from t_user; </select> <insert id="addUserInfo" parameterType="com.CCBK.app.entity.User"> insert into t_user (id, name ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR} ) </insert> <delete id="delUserInfo" parameterType="java.lang.Integer"> delete from t_user where id = #{id,jdbcType=INTEGER} </delete> </mapper>
application.properties
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/ccbk?useUnicode\=true&characterEncoding\=gbk&zeroDateTimeBehavior\=convertToNull spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # \u521D\u59CB\u5316\u5927\u5C0F\uFF0C\u6700\u5C0F\uFF0C\u6700\u5927 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 # \u914D\u7F6E\u83B7\u53D6\u8FDE\u63A5\u7B49\u5F85\u8D85\u65F6\u7684\u65F6\u95F4 spring.datasource.maxWait=60000 mybatis.mapper-locations=classpath*:mapper/*Mapper.xml mybatis.type-aliases-package=com.CCBK.app.entity
pom.xml
<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.ccbookkeeping</groupId> <artifactId>CCBookKeeping</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>CCBookKeeping</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath /> </parent> <dependencies> <!-- 上邊引入 parent,所以 下邊無需指定版本 --> <!-- 包含 mvc,aop 等jar資源 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</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.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> </dependency>
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-joda</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-parameter-names</artifactId> </dependency> <!-- alibaba的druid數據庫鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3、總結
折騰了一上午的結果,主要問題是本身不仔細和不認真看官方文檔致使的,碰到的問題以下
1.自定的controller不能訪問
前臺沒法訪問,報錯以下
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Mar 25 11:51:37 CST 2018 There was an unexpected error (type=Not Found, status=404). No message available
2.controller中@autowried注入service報錯
找不到UserService,報錯以下
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in com.CCBK.app.controller.UserController required a bean of type 'com.CCBK.app.service.UserService' that could not be found.
Action:
Consider defining a bean of type 'com.CCBK.app.service.UserService' in your configuration.
以上兩個問題歸根結底是由於main方法所在的類App.java放的位置不對,此項目中App.java要和其餘的包放在同級
具體springboot的詳細使用還在探索中,若是以上有理解錯誤的地方但願各位前輩指正🙏🙏