SpringBoot+Mybatis框架Demo

来源:互联网 发布:mysql linux登陆命令 编辑:程序博客网 时间:2024/06/06 18:16

1、使用Intellij IDEA 2016.3 新建一个SpringBoot项目


这样就创建了一个简单的SpringBoot项目了。

2、添加jar包

<properties>   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>   <java.version>1.8</java.version>   <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>   <mysql-connector.version>5.1.39</mysql-connector.version></properties><dependencies>   <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>   </dependency>   <!-- 监控 -->   <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-actuator</artifactId>   </dependency>   <dependency>      <groupId>javax.servlet</groupId>      <artifactId>jstl</artifactId>   </dependency>   <!-- Provided -->   <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>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-tomcat</artifactId>      <scope>provided</scope>   </dependency>   <!-- Spring Boot Mybatis 依赖 -->   <dependency>      <groupId>org.mybatis.spring.boot</groupId>      <artifactId>mybatis-spring-boot-starter</artifactId>      <version>${mybatis-spring-boot.version}</version>   </dependency>   <!-- MySQL 连接驱动依赖 -->   <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>      <version>${mysql-connector.version}</version>   </dependency>   <!--引入redis-->   <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-redis</artifactId>      <version>1.4.7.RELEASE</version>   </dependency>   <!--引入定时任务-->   <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter</artifactId>   </dependency>   <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-test</artifactId>      <scope>test</scope>   </dependency>   <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-devtools</artifactId>      <optional>true</optional>   </dependency></dependencies>
3、配置application.yml文件

新建项目时文件名是application.properties需要重命名下

spring:  datasource:    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8    username: root    password: root    driver-class-name: com.mysql.jdbc.Driver  redis:    database: 0 #Redis数据库索引    host: 127.0.0.1 #Redis服务器地址    port: 6379 #Redis端口号    password: #Redis密码    pool:      max-active: 8 #Redis连接池最大连接数      max-wait: -1 #Redis连接池最大阻塞等待时机 负值代表没有限制      max-idle: 8 #Redis最大空闲连接      min-idle: 0 #Redis最新空闲连接    timeout: 0 #Redis连接超时时间mybatis:  typeAliasesPackage: com.yjx.domain  mapperLocations: classpath:mapper/*.xmllogging:  path: /log  level: INFO
3、代码实现

domain实体

private Integer uid;private String username;private String password;private String name;
dao层接口
List<UserInfo> findAll();UserInfo findById(Integer id);UserInfo findByName(String name);/**通过username查找用户信息;*/public UserInfo findByUsername(String username);
service层实现
@Autowiredprivate UserInfoDao userInfoDao;@Overridepublic UserInfo findByUsername(String username) {    System.out.println("UserInfoServiceImpl.findByUsername()");    return userInfoDao.findByUsername(username);}public List<UserInfo> findAll(){    return userInfoDao.findAll();}@Overridepublic UserInfo findById(Integer id) {    return userInfoDao.findById(id);}@Overridepublic UserInfo findByName(String name) {    return userInfoDao.findByName(name);}
controller层
@RequestMapping("user")@RestControllerpublic class UserController {    @Autowired    private UserInfoServiceImpl userInfoService;    @GetMapping("/list/all")    @Cacheable(value="user-key")    public Object listAll() {        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");        return userInfoService.findAll();//    }    @RequestMapping("/getUser/id/{id}")    @Cacheable(value="user-key")    public UserInfo getUser(@PathVariable("id") Integer id ) {        UserInfo user=userInfoService.findById(id);        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");        return user;    }    @RequestMapping("/getUser")    @Cacheable(value="user-key")    public List<UserInfo> findAll() {        List<UserInfo>  user=userInfoService.findAll();        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");        return user;    }    @Cacheable(value="user-key")    @Scheduled(cron="*/6 * * * * ?")    public Object jobListAll() {        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");        return userInfoService.findAll();//@Scheduled(cron="*/6 * * * * ?")    }    @RequestMapping("/getUser/name/{name}")    @Cacheable(value="user-key")    public UserInfo getUserByName(@PathVariable("name") String name) {        UserInfo user=userInfoService.findByName(name);        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");        return user;    }    @RequestMapping("/uid")    String uid(HttpSession session) {        UUID uid = (UUID) session.getAttribute("uid");        if (uid == null) {            uid = UUID.randomUUID();        }        session.setAttribute("uid", uid);        return session.getId();    }
启动类
@MapperScan("com.yjx.dao")@SpringBootApplication@EnableSchedulingpublic class SpringbootMybatisApplication {   public static void main(String[] args) {      SpringApplication.run(SpringbootMybatisApplication.class, args);   }}
sql文件
<mapper namespace="com.yjx.dao.UserInfoDao" >    <resultMap id="BaseResultMap" type="com.yjx.domain.UserInfo" >        <id column="uid" property="uid"  />        <result column="username" property="username"  />        <result column="name" property="name"  />        <result column="password" property="password"  />    </resultMap>    <sql id="Base_Column_List" >        uid, username, password, name    </sql>    <select id="findAll" resultMap="BaseResultMap" >        select        <include refid="Base_Column_List" />        from user_info    </select>    <select id="findById" resultMap="BaseResultMap" parameterType="java.lang.Integer" >        select        <include refid="Base_Column_List" />        from user_info        where uid = #{id,jdbcType=INTEGER}    </select>    <select id="findByUsername" resultMap="BaseResultMap" parameterType="java.lang.String" >        select        <include refid="Base_Column_List" />        from user_info        where username = #{username,jdbcType=VARCHAR}    </select>    <select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String" >        select        <include refid="Base_Column_List" />        from user_info        where username = #{name,jdbcType=VARCHAR}    </select></mapper>
4、运行效果

5、源码下载

源码下载