springboo機集成mybatis及mysqljava
1,添加依賴mysql
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <!-- 項目信息 begin --> <groupId>com.microservice</groupId> <artifactId>spring-web</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-web</name> <url>http://maven.apache.org</url> <!-- 項目信息end --> <!-- 屬性配置 begin --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!-- 屬性配置end --> <!-- 父依賴 begin --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <!-- 父依賴 end --> <dependencies> <!-- 添加web包 begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- 該包中包含requestMapping restController 等註解 --> <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> <!-- 添加web包 end --> <!-- mybatis依賴 begin --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- mybatis依賴 end --> <!-- mysql數據庫配置 begin --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mysql數據庫配置 end --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
添加該配置時,須要注意mybatis和spring版本衝突問題。若是發生衝突,會致使一些奇怪的問題,例如註解(@Mapper)不成功。要解決該問題,能夠參考網址https://start.spring.io/,配置版本。web
2,添加配置信息redis
#系統中用到的參數配置 編碼格式 com.interview.question=springboot有哪些配置的註解 #數據庫鏈接配置信息 spring.datasource.driverClassName = com.mysql.jdbc.Driver #spring.datasource.url = jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8 spring.datasource.url = jdbc:mysql://localhost:3306/shop spring.datasource.username = root spring.datasource.password = root #mybatis數據庫映射文件配置 mybatis.config-locations=classpath:mybatis/mybatis-config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis-config.xml————數據類型的別名spring
<configuration> <typeAliases> <typeAlias alias="Integer" type="java.lang.Integer" /> <typeAlias alias="Long" type="java.lang.Long" /> <typeAlias alias="HashMap" type="java.util.HashMap" /> <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" /> <typeAlias alias="ArrayList" type="java.util.ArrayList" /> <typeAlias alias="LinkedList" type="java.util.LinkedList" /> </typeAliases> </configuration>
*.xml——配置mybatis數據庫映射文件sql
例如UserMapper.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="org.spring.web.mapper.UserMapper" >apache
<resultMap id="BaseResultMap" type="org.spring.web.entity.User" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="passWord" property="passWord" jdbcType="VARCHAR" />
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List" >
id, user_name,age, passWord
</sql>
<!-- 查詢用戶 -->
<select id="selectUserById" resultMap="BaseResultMap" parameterType="Integer">
select
<include refid="Base_Column_List" />
from user where id=#{id}
</select>
<update id="updateUser" parameterType="org.spring.web.entity.User" >
UPDATE
user
SET
<if test="age != null and age!=''">age = #{age},</if>
<if test="passWord != null and passWord!=''">passWord = #{passWord},</if>
user_name = #{userName}
WHERE
id = #{id}
</update>
</mapper>springboot
3,XXMapper.xml對應的XXMapper.java類,該類和XXMapper.xml是對應的,爲接口,其中的方法名和XXMapper.xml中sql的id是一致的。restful
package org.spring.web.mapper; import org.apache.ibatis.annotations.Mapper; import org.spring.web.entity.User; import org.springframework.stereotype.Component; /** * * 項目名稱:spring-web * 類名稱:UserMapper * 類描述: * 建立人:john * 建立時間:2018年7月28日 上午11:57:18 * 修改人:john * 修改時間:2018年7月28日 上午11:57:18 * 修改備註: * @version * */ @Mapper //@Component public interface UserMapper { /* * 用戶新增 */ public int inserUser(User user); public User selectUserById(Integer id); public int updateUser(User user); }
@Mapper 注入該類,也能夠在啓動類直接添加 @MapperScan("org.spring.web.mapper") ,其中的參數是XXMapper.java所在的位置。@MapperScan("")和在XXMapper.java上添加@Mapper的做用是同樣的。
4,編寫數據庫服務接口層
package org.spring.web.service; import org.spring.web.entity.User; import org.springframework.stereotype.Service; /** * * 項目名稱:spring-web * 類名稱:UserService * 類描述: * 建立人:john * 建立時間:2018年7月28日 上午11:52:50 * 修改人:john * 修改時間:2018年7月28日 上午11:52:50 * 修改備註: * @version * */ public interface UserService { public int inserUser(User user); public User selectUserById(Integer id); public int updateUser(User user); public String getUser(); }
數據服務接口實現層
package org.spring.web.service.serviceImpl; import java.util.concurrent.TimeUnit; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spring.web.entity.User; import org.spring.web.mapper.UserMapper; import org.spring.web.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; /** * * 項目名稱:spring-web * 類名稱:UserServiceImpl * 類描述: * 建立人:john * 建立時間:2018年7月28日 上午11:53:16 * 修改人:john * 修改時間:2018年7月28日 上午11:53:16 * 修改備註: * @version * */ @Service public class UserServiceImpl implements UserService{ private static Logger logger =LoggerFactory.getLogger(UserServiceImpl.class); @Autowired private UserMapper userMapper; @Autowired private RedisTemplate redisTemplate; public int inserUser(User user) { return 0; } public User selectUserById(Integer id) { User user=userMapper.selectUserById(id); System.out.println("根據用戶ID>>"+id+"查詢的用戶信息>>>>>."+user); return user; } @Override public int updateUser(User user) { return userMapper.updateUser(user); } @Override public String getUser() { User user=new User(); user.setId(22); user.setUserName("interview"); ValueOperations<String, User> operations=redisTemplate.opsForValue(); operations.set("com.yfli", user); operations.set("com.neo.f", user,1,TimeUnit.SECONDS); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //redisTemplate.delete("com.neo.f"); boolean exists=redisTemplate.hasKey("com.neo.f"); if(exists){ System.out.println("exists is true"); }else{ System.out.println("exists is false"); } User redisUser=operations.get("com.yfli"); return redisUser.toString(); } }
數據接口實現層添加@Service註解就能夠了,接口層不用添加註解。若是實現層不添加註解,main方法啓動的時候報錯。
5,controller層
package org.spring.web.controller; import org.spring.web.entity.User; import org.spring.web.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * * 項目名稱:spring-web * 類名稱:UserController * 類描述: * 建立人:john * 建立時間:2018年7月28日 下午12:15:36 * 修改人:john * 修改時間:2018年7月28日 下午12:15:36 * 修改備註: * @version * */ @RestController @RequestMapping("/user") public class UserController { @Autowired UserService userService; @RequestMapping("/selectByUserId") public User selectByUserId(Integer id){ id=1; return userService.selectUserById(id); } @RequestMapping("/updateUser") public int updateUser(User user){ user.setPassword("222"); user.setUserName("你好"); user.setAge(110); user.setId(1); return userService.updateUser(user); } @RequestMapping("/getRedisUser") public String getRedisUser(){ return userService.getUser(); } }
controller層注入接口層,直接調用服務層的邏輯便可,程序的處理邏輯放在服務實現層。controller只須要接受參數,處理簡單的轉化邏輯便可。
重點:設計restfule接口風格(待完善)