package com.corey.controller;
import com.corey.api.UserService; import com.corey.model.Person; import com.corey.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;
@RestController//与springmvc不同,注解是RestController public class AccountController {
@Autowired UserService userService;
@RequestMapping("/index") public String toIndex(){
return "hellworld!"; }
/** * 返回jsp * @param model * @return */ @RequestMapping("/hello") public String helloJsp(Model model){ System.out.println("HelloController.helloJsp().hello=hello"); model.addAttribute("hello", "你好"); return "hello"; }
/** * json格式请求 * @return */ @RequestMapping("/json") @ResponseBody public Person json(){ Person person=new Person(); person.setName("倪先华"); return person; }
/** * 连接数据库获取实体 * @param id * @return * @throws Exception */ @RequestMapping("/getUserbyId") public User getUserbyId(Integer id) throws Exception{
return userService.getUserById(id); }
}二、api层
package com.corey.api;
import com.corey.model.User;
public interface UserService {
public User getUserById(Integer id) throws Exception;
}三、service层
package com.corey.service; import com.corey.api.UserService; import com.corey.dao.UserMapper; import com.corey.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; public User getUserById(Integer id) throws Exception { return userMapper.getUserById(id); } } 四、dao层 package com.corey.dao; import com.corey.model.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.stereotype.Repository; @Mapper//dao层一定要加mapper注解 public interface UserMapper { @Select("select * from sys_user where id = #{id}") public User getUserById(Integer id); }四、appstart
package com.corey; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(scanBasePackages="com.corey")//控制层、api、服务和dao层一定要在该包下,否则无法扫描 @MapperScan("cn.itsource.springboot.mybatis.mapper")//别名:mybatis.type-aliases-package=cn.itsource.springboot.ssm.domain//支持mybatis持久层 public class AppStart { public static void main(String args[]){ SpringApplication.run(AppStart.class,args); } } 五、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.corey</groupId> <artifactId>springboot</artifactId> <packaging>pom</packaging> <version>1.0</version> <modules> <module>common</module> <module>model</module> <module>api</module> <module>server</module> <module>web</module> </modules> <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <properties> <!-- springboot版本号 --> <springboot.version>1.4.1.RELEASE</springboot.version> <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 --> <java.version>1.8</java.version> </properties> <dependencies> <!-- 依赖配置--> <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${springboot.version}</version> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- servlet 依赖. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat 的支持. --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--mysql,jdbc--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : 如果没有该项配置,热部署devtools不会起作用,即应用不会restart --> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>五、截图与访问