目錄結構:

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.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mybatis的依賴配置--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</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-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.28</version> </dependency> <!-- alibaba的druid數據庫連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.11</version> </dependency> <!--json的相關配置--> <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> <!-- 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.1.2</version> </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> <!-- mybatis generator 自動生成代碼插件 --> <!--<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin>--> </plugins> </build> </project>
UserMapping.xml文件:
<?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.example.demo.mapper.UserMapper" >
    <resultMap id="BaseResultMap" type="com.example.demo.model.User" >
        <id column="user_id" property="userId" jdbcType="INTEGER" />
        <result column="user_name" property="userName" jdbcType="VARCHAR" />
       <!-- <result column="password" property="password" jdbcType="VARCHAR" />-->
        <result column="phone" property="phone" jdbcType="VARCHAR" />
    </resultMap>
    <sql id="Base_Column_List" >
        user_id, user_name, phone
    </sql>
    <!-- 查詢全部的方法 -->
    <select id="getAllUser" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from MAC_USER
    </select>
    <!--<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from t_user
        where user_id = #{userId,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.winter.model.User" >
        insert into t_user (user_id, user_name, password,
        phone)
        values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
        #{phone,jdbcType=VARCHAR})
    </insert>
    <insert id="insertSelective" parameterType="com.winter.model.User" >
        insert into t_user
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="userId != null" >
                user_id,
            </if>
            <if test="userName != null" >
                user_name,
            </if>
            <if test="password != null" >
                password,
            </if>
            <if test="phone != null" >
                phone,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <if test="userId != null" >
                #{userId,jdbcType=INTEGER},
            </if>
            <if test="userName != null" >
                #{userName,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null" >
                #{phone,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.winter.model.User" >
        update t_user
        <set >
            <if test="userName != null" >
                user_name = #{userName,jdbcType=VARCHAR},
            </if>
            <if test="password != null" >
                password = #{password,jdbcType=VARCHAR},
            </if>
            <if test="phone != null" >
                phone = #{phone,jdbcType=VARCHAR},
            </if>
        </set>
        where user_id = #{userId,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.winter.model.User" >
        update t_user
        set user_name = #{userName,jdbcType=VARCHAR},
        password = #{password,jdbcType=VARCHAR},
        phone = #{phone,jdbcType=VARCHAR}
        where user_id = #{userId,jdbcType=INTEGER}
    </update>-->
</mapper> 
        application.yml數據庫相關配置文件
server: port: 8080 spring: datasource: name: test url: jdbc:mysql://192.168.10.17:3306/mac_demo username: root password: 123456 # 使用druid數據源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.example.demo.model #pagehelper分頁插件 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
User.java
package com.example.demo.model; public class User { private Integer userId ; private String userName; private String phone ; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
UserMapper.xml
package com.example.demo.mapper; import com.example.demo.model.User; import java.util.List; public interface UserMapper { List<User> getAllUser() ; }
UserService.java
package com.example.demo.service; import com.example.demo.model.User; import java.util.List; public interface UserService { //List<User> getAllUser(int pageNum,int pageSize) ; List<User> getAllUser() ; }
UserServiceImpl.java
package com.example.demo.service.impl; import com.example.demo.mapper.UserMapper; import com.example.demo.model.User; import com.example.demo.service.UserService; import com.github.pagehelper.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service(value="userService") public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper ; /* @Override public List<User> getAllUser(int pageNum,int pageSize) { PageHelper.startPage(pageNum,pageSize) ; return userMapper.getAllUser(); }*/ @Override public List<User> getAllUser(){ return userMapper.getAllUser() ; } }
UserController.java
package com.example.demo.controller; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService ; //分頁的寫法 /*@ResponseBody @RequestMapping(value = "/all/{pageNum}/{pageSize}",produces = {"application/json;charset=UTF-8"}) public Object getAllUser(@PathVariable("pageNum") int pageNum,@PathVariable("pageSize") int pageSize){ return userService.getAllUser(pageNum,pageSize) ; }*/ @ResponseBody @RequestMapping(value = "/all",produces = {"application/json;charset=UTF-8"}) public Object getAllUser(){ return userService.getAllUser() ; } }
DemoAppliction.java這個是springboot的核心主文件
package com.example.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication//組合注解 @EnableAutoConfiguration//隱式地為特定項目定義了一個基本“搜索包”。 @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
harddream!!!
