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: INFO3、代码实现
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、源码下载
源码下载
阅读全文
0 0
- SpringBoot+Mybatis框架Demo
- SpringBoot + Mybatis 整合demo
- 使用springBoot框架+maven+mybatis 实现的小demo
- SpringBoot集成MyBatis框架
- springboot+mybatis+shiro框架
- mybatis框架学习demo
- SpringBoot+Mybatis学习Demo问题总结1
- 初学者mysql+mybatis-springboot的demo
- springboot整合mybatis一个简单的demo
- 本地环境:mycat+mybatis+springboot实现demo
- springboot和mybatis两个框架结合教程
- 简单的SpringBoot+Mybatis框架整合
- 一步一步搭建springboot+mybatis+jta框架
- 【Mybatis学习】利用SpringBoot搭建SSM框架
- maven实现springboot-mybatis框架(1)
- maven实现springboot-mybatis框架(2)
- springboot、mybatis、thymeleaf、bootstrap整合框架
- Springboot+mybatis+Jsp+ztree2.5简单的小demo
- 第五周项目一
- OpenVPN初始安装在D盘,卸载重装后装在C盘,安装完成后运行的时候却提示启动 OpenVPN GUI 时失败,提示“CreateProcess Failed, exe
- 数组去重的几种方法,从数组中选择出互不重复的5个数
- 第5周项目 项目1
- 用Python学《微积分B》(微分方程)
- SpringBoot+Mybatis框架Demo
- 写给所有程序员_起个好名字是成功的一半
- 整理NLPIR基本功能函数
- ARC078
- 昂昂
- 云数据库管理与数据迁移可以认证了,是时候证明自己了
- C# 索引器
- 第七周 【项目1-建立顺序环形队列算法库】
- android AccessibilityService的学习和应用