springboot + mybatis(注解版)

1.文件目录


2.App.java
package com.app ;

import org.mybatis.spring.annotation.MapperScan ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.annotation.ComponentScan ;

/**
* Created by Administrator on 2018/3/14.
*/
@ComponentScan ( basePackages = { "com.controller" , "com.service" })
@MapperScan ( basePackages = "com.mapper" )
@EnableAutoConfiguration
public class App {

public static void main (String[] args) {
SpringApplication. run (App. class, args) ;
}
}

3.controller
package com.controller ;

import com.pojo.User ;
import com.service.UserService ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.stereotype.Controller ;
import org.springframework.web.bind.annotation.ResponseBody ;

import java.util.List ;

/**
* Created by Administrator on 2018/3/14.
*/
@Controller
public class UserController {

@Autowired
private UserService userService ;
@RequestMapping ( "/index" )
public String index (){
return "index" ;
}

@ResponseBody
@RequestMapping ( "/getUser" )
public List<User> getUser (){
List<User> users = userService .getAllUser() ;
return users ;
}
}
4.mapper
package com.mapper ;

import com.pojo.User ;
import org.apache.ibatis.annotations.Mapper ;
import org.apache.ibatis.annotations.Select ;

import java.util.List ;

/**
* Created by Administrator on 2018/3/14.
*/
@Mapper
public interface UserMapper {

@Select ( "select * from user" )
List<User> getAllUser () ;
}

5.pojo
package com.pojo ;

/**
* Created by Administrator on 2018/3/14.
*/
public class User {
private Integer id ;
private String username ;
private String mobile ;
private Integer role ;
private Integer status ;

public Integer getId () {
return id ;
}

public void setId (Integer id) {
this . id = id ;
}

public String getUsername () {
return username ;
}

public void setUsername (String username) {
this . username = username ;
}

public String getMobile () {
return mobile ;
}

public void setMobile (String mobile) {
this . mobile = mobile ;
}

public Integer getRole () {
return role ;
}

public void setRole (Integer role) {
this . role = role ;
}

public Integer getStatus () {
return status ;
}

public void setStatus (Integer status) {
this . status = status ;
}
}

6.UserService
package com.service ;

import com.pojo.User ;

import java.util.List ;

/**
* Created by Administrator on 2018/3/14.
*/

public interface UserService {

List<User> getAllUser () ;
}

7.UserServiceImpl

package com.service ;

import com.mapper.UserMapper ;
import com.pojo.User ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;

import java.util.List ;

/**
* Created by Administrator on 2018/3/15.
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper usermapper ;
@Override
public List<User> getAllUser () {
return usermapper .getAllUser() ;
}
}

8.application.properties
spring.mvc.view.prefix = /WEB-INF/jsp/
spring.mvc.view.suffix = .jsp

spring.datasource.url = jdbc:mysql://localhost:3306
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name = com.mysql.jdbc.Driver

9.index.jsp

10.web.xml
略。(保留创建出来的样子)

11.pom.xml
<? xml version ="1.0" encoding ="UTF-8" ?>
<modelVersion> 4.0.0 </modelVersion>

<groupId> com.springboot </groupId>
<artifactId> springboot-mybatis </artifactId>
<version> 1.0-SNAPSHOT </version>



<parent>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-parent </artifactId>
<version> 1.5.3.RELEASE </version>
</parent>

<dependencies>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-web </artifactId>
</dependency>
<dependency>
<groupId> javax.servlet </groupId>
<artifactId> jstl </artifactId>
</dependency>
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId> spring-boot-starter-tomcat </artifactId>
</dependency>
<dependency>
<groupId> org.apache.tomcat.embed </groupId>
<artifactId> tomcat-embed-jasper </artifactId>
</dependency>
<dependency>
<groupId> mysql </groupId>
<artifactId> mysql-connector-java </artifactId>
</dependency>
<dependency>
<groupId> org.mybatis.spring.boot </groupId>
<artifactId> mybatis-spring-boot-starter </artifactId>
<version> 1.1.1 </version>
</dependency>



</dependencies>

</project>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值