91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot通過yml方式整合Mybatis的方法

發布時間:2020-08-13 11:14:44 來源:億速云 閱讀:183 作者:小新 欄目:編程語言

這篇文章主要介紹SpringBoot通過yml方式整合Mybatis的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、項目目錄結構

  注意這里Application文件的位置,它是與controller、entity、mapper、service等包處于并列的關系。

SpringBoot通過yml方式整合Mybatis的方法

二、數據庫文件

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_user_00
-- ----------------------------
DROP TABLE IF EXISTS `t_user_00`;
CREATE TABLE `t_user_00` (
 `id` int(0) NOT NULL AUTO_INCREMENT,
 `user_id` int(0) NOT NULL,
 `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
 `age` int(0) NOT NULL,
 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

三、pom.xml

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.13.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.gougou</groupId>
 <artifactId>shardingjdbc-shardingtable-demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>shardingjdbc-shardingtable-demo</name>
 <description>shardingjdbc-shardingtable-demo</description>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.3.2</version>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

</project>

四、application.yml

# 數據源
spring:
 application:
 name: shardingjdbc-shardingtable-demo
 datasource:
 url: jdbc:mysql://localhost:3306/sharding_0&#63;serverTimezone=UTC
 username: root
 password: root
 driver-class-name: com.mysql.jdbc.Driver
 dbcp2:
  min-idle: 5        # 數據庫連接池的最小維持連接數
  initial-size: 5       # 初始化連接數
  max-total: 5        # 最大連接數
  max-wait-millis: 150      # 等待連接獲取的最大超時時間

# mybatis配置
mybatis:
 mapper-locations: classpath:mapper/*.xml # mapper映射文件位置
 type-aliases-package: com.gouggou.shardingtable.entity # 實體類所在的位置
 configuration:
 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #用于控制臺打印sql語句

五、啟動類Application

  說明:

  1、@MapperScan的:掃描mapper接口的位置

  2、@ComponentScan:如果Application文件的位置不是與controller、entity、mapper、service等包處于并列的關系。就要用此注解,否則可以不用;

@MapperScan("com.gouggou.shardingtable.mapper")
@SpringBootApplication
public class Application {

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

}

六、controller

@RequestMapping("student")
@RestController
public class UserController {

 @Autowired
 private UserService userService;

 @RequestMapping("save")
 public String save() {
  User user = new User();
  user.setUserId(new Random().nextInt( 1000 ) + 1);
  user.setName("張三"+user.getUserId());
  user.setAge(new Random().nextInt( 80 ) + 1);
  userService.insert(user);
  return user.getName()+"創建成功!";
 }

}

七、service

public interface UserService {
 Integer insert(User u);
 List<User> findAll();
 List<User> findByUserIds(List<Integer> userIds);
}
@Service
public class UserServiceImpl implements UserService {

 @Autowired
 private UserMapper userMapper;

 @Override
 public Integer insert(User u) {
  return userMapper.insert(u);
 }

 @Override
 public List<User> findAll() {
  return userMapper.findAll();
 }

 @Override
 public List<User> findByUserIds(List<Integer> userIds) {
  return userMapper.findByUserIds(userIds);
 }
}

八、entity

@Data
public class User implements Serializable {
 private static final long serialVersionUID = -5514139686858156155L;
 private Integer id;
 private Integer userId;
 private String name;
 private Integer age;
}

九、Mapper

@Repository
public interface UserMapper {
 Integer insert(User u);
 List<User> findAll();
 List<User> findByUserIds(List<Integer> userIds);
}
<&#63;xml version="1.0" encoding="UTF-8" &#63;>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.gouggou.shardingtable.mapper.UserMapper" >
 <resultMap id="resultMap" type="com.gouggou.shardingtable.entity.User" >
  <id column="id" property="id" jdbcType="INTEGER" />
  <result column="user_id" property="userId" jdbcType="INTEGER" />
  <result column="name" property="name" jdbcType="VARCHAR" />
  <result column="age" property="age" jdbcType="INTEGER" />
 </resultMap>

 <insert id="insert">
 insert into t_user_00 (user_id,name,age) values (#{userId},#{name},#{age})
 </insert>

 <select id="findAll" resultMap="resultMap">
  select <include refid="columnsName"/> from t_user_00
 </select>

 <select id="findByUserIds" resultMap="resultMap">
  select <include refid="columnsName"/> from t_user_00 where user_id in (
  <foreach collection="list" item="item" separator=",">
   #{item}
  </foreach>
  )

 </select>

 <sql id="columnsName">
  id,user_id,name,age
 </sql>
</mapper>

十、遇到的問題

  1、idea 右鍵無java class選項

  2、maven倉庫中產生后綴是LastUpdated的文件

以上是SpringBoot通過yml方式整合Mybatis的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

南投县| 乳山市| 仙居县| 东莞市| 增城市| 古交市| 德阳市| 盐山县| 永仁县| 佳木斯市| 兴国县| 承德市| 合山市| 正镶白旗| 城口县| 乐都县| 贡山| 华安县| 江城| 烟台市| 聂拉木县| 拉萨市| 马尔康县| 乡城县| 重庆市| 延庆县| 海门市| 漠河县| 齐齐哈尔市| 蓬安县| 鹿邑县| 常山县| 那曲县| 金门县| 五大连池市| 扎兰屯市| 香港| 沅陵县| 贵南县| 临泉县| 沙洋县|