Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of writing hello world code by MyBatis

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of MyBatis writing hello world code". In the daily operation, I believe that many people have doubts about the method of writing hello world code in MyBatis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "MyBatis method of writing hello world code". Next, please follow the editor to study!

Directory preparation SQL script download dependent jar package preparation log configuration file & jdbc configuration writing hello world preparation SQL script

The database I use here is MySQL 7. The student (student table), course (course schedule) and score (score table) scripts are designed as follows:

SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0Mutual-Table structure for course-- DROP TABLE IF EXISTS `story` CREATE TABLE `room` (`course_ id` int (10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` varchar (255th) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT'', PRIMARY KEY (`course_ id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic -Records of course-- INSERT INTO `imperial `VALUES (1, 'benevolent'); INSERT INTO `imperial `VALUES (2, 'overbearing'); INSERT INTO `imperial `VALUES (3, 'Art of War'); INSERT INTO `ang`VALUES (4, 'riding') INSERT INTO `archery 'VALUES (5,' Archery'); INSERT INTO 'Archery' VALUES (6, 'Defense'); INSERT INTO `Jing`VALUES (7, 'Green Dragon Yanyue Dao'); INSERT INTO `Jing`VALUES (8, 'Zhangba Snake Spear'); INSERT INTO `Jing`VALUES (9, 'long gun'); INSERT INTO `ang`VALUES (10, 'checks and balances'); INSERT INTO `ang`VALUES (11, 'medical skills'); INSERT INTO `Jing`VALUES (12, 'stargazing'); INSERT INTO `Jing`VALUES (13, 'Astronomy') INSERT INTO `Qing`VALUES (14, 'Geography'); INSERT INTO `Qing`VALUES (15, 'weapons'); INSERT INTO `Jing`VALUES (16, 'double Sword'); INSERT INTO `Jing`VALUES (17, 'Gentian'); INSERT INTO `Qing`VALUES (18, 'Iron Horseman') -Table structure for score---DROP TABLE IF EXISTS `score` CREATE TABLE `score` (`student_ id` int (11) UNSIGNED NOT NULL AUTO_INCREMENT, `student_ id` int (10) UNSIGNED NOT NULL, `course_ id` int (10) UNSIGNED NOT NULL, `score` decimal (65,0) UNSIGNED NOT NULL, PRIMARY KEY (`score_ id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic -Records of score---INSERT INTO `score` VALUES (1,1,7,99); INSERT INTO `score`VALUES (2,2,8,95); INSERT INTO `score`VALUES (3,3,17,99); INSERT INTO `score`VALUES (4,4,5,90) INSERT INTO `score`VALUES (5, 5, 18, 96); INSERT INTO `score` VALUES (6, 5, 9, 90); INSERT INTO `score` VALUES (7, 4, 9, 96); INSERT INTO `score` VALUES (8, 6, 1, 99); INSERT INTO `score` VALUES (9, 6, 3, 60); INSERT INTO `score`VALUES (10, 6, 16, 80); INSERT INTO `score` VALUES (11, 7, 12, 96); INSERT INTO `score` VALUES (12, 7, 13, 93); INSERT INTO `score` VALUES (13, 7, 14, 98) INSERT INTO `score`VALUES (14,8,12,96); INSERT INTO `score`VALUES (15,9,10,91); INSERT INTO `score`VALUES (16,10,2,99); INSERT INTO `score`VALUES (17,10,3,95); INSERT INTO `score`VALUES (18,11,100); INSERT INTO `score`VALUES (19,12,15,93) -Table structure for student---DROP TABLE IF EXISTS `student` CREATE TABLE `student` (`create_ id` int (11) UNSIGNED NOT NULL AUTO_INCREMENT, `name` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `sex` varchar (255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `age` tinyint (4) UNSIGNED NOT NULL, `create_ datetime (0) NOT NULL ON UPDATE CURRENT_TIMESTAMP (0), `modify_ time` datetime (0) NOT NULL ON UPDATE CURRENT_TIMESTAMP (0) PRIMARY KEY (`student_ id`) USING BTREE) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic -Records of student-- INSERT INTO `student` VALUES (1, 'Guan Yu', 'male', 55, '2019-10-31 10 purge 23 VALUES 50 minutes,' 2019-10-31 10 purge 23 purl 52') INSERT INTO `student` VALUES (2, 'Zhang Fei', 'male', 52, '2019-10-31 10 INSERT INTO 25 VALUES (3,' Zhaoyun', 'male', 50, '2019-10-31 10-31 10 purge 2414 37),' 2019-10-31 10 14 24 INSERT INTO 39') INSERT INTO `student` VALUES (4, 'Huang Zhong', 'male', 60, '2019-10-31 10 INSERT INTO 25 VALUES (5,' Ma Chao', 'male', 40, '2019-10-31 10 INSERT INTO 25 INSERT INTO 23') INSERT INTO `student` VALUES (6, 'Liu Bei', 'male', 55, '2019-10-31 10 INSERT INTO 25 VALUES (7,' Zhuge Liang', 'male', 30, '2019-10-31 10-26 VALUES 05,' 2019-10-31 10-26 VALUES 07') INSERT INTO `student` VALUES (8, 'Guojia', 'male', 25, '2019-10-31 10 INSERT INTO 26 VALUES (9,' Sun Quan', 'male', 27, '2019-10-31 10 INSERT INTO 26 VALUES 51', '2019-10-31 10 INSERT INTO 26 VALUES 53') INSERT INTO `student` VALUES (10, 'Cao Cao', 'male', 60, '2019-10-31 10 INSERT INTO 2708mm,' 2019-10-31 10 INSERT INTO 27-26'); INSERT INTO `student` 20 (11, 'Hua Tuo', 'male', 70, '2019-10-31 10 INSERT INTO 2722, 2019-10-31 10) INSERT INTO `student` VALUES (12, 'Sun Shangxiang', 'female', 25, '2019-10-31 10 SET FOREIGN_KEY_CHECKS 27 SET FOREIGN_KEY_CHECKS 42),' 2019-10-31 10 SET FOREIGN_KEY_CHECKS = 1; download the dependent jar package you need

The project build tool I use here is maven. The following code only selects the dependencies tag:

Org.mybatis mybatis 3.5.3 junit junit 4.12 test org.projectlombok lombok 1.18.10 provided mysql mysql-connector-java 6.0.6 log4j log4j 1.2.17 org.slf4j slf4j-api 1.7.28 prepare log configuration file & jdbc configuration LOG uses log4j (log4j support is included in mybatis log support) lo4j.propertieslog4j.rootLogger=TRACE Stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%-d {yyyy-MM-dd HH:mm:ss.SSS} [% t]-[% c]-[% p]% m%nlog4j.logger.org.apache.ibatis=TRACEjdbc.properties configure jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mybatis-demo?characterEncoding=utf8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghaijdbc.user=xxxjdbc.password=xxx

Note that you need to specify the serverTimezone parameter when using mysql-connector-java 6 or later, otherwise an error will be reported at startup.

Write hello world code 1. Write the entity class Student classpackage org.hepeng.demo.mybatis.entity;import lombok.Data;import java.util.Date;/** * @ author hepeng * / @ Datapublic class Student {private Integer studentId; private String name; private String sex; private Byte age; private Date createTime; private Date modifyTime;} Course classpackage org.hepeng.demo.mybatis.entity;import lombok.Data;/** * @ author hepeng * / @ Datapublic class Course {private Integer courseId Private String name;} Score classpackage org.hepeng.demo.mybatis.entity;import lombok.Data;/** * @ author hepeng * / @ Datapublic class Score {private Integer scoreId; private Integer studentId; private Integer courseId; private Double score;} 2. Write mybatis configuration file mybatis-config.xml 3. Write the mapper configuration file to create a new mapper directory under the resources directory, and create a new StudentMapper.xml (mapper/StudentMapper.xml) select * from student 4 under the mapper directory. Write test code package org.hepeng.demo.mybatis;import lombok.extern.slf4j.Slf4j;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.junit.Assert;import org.junit.Before;import java.io.InputStream;import java.util.List;/** * @ author hepeng * / @ Slf4jpublic class Test {SqlSessionFactory sqlSessionFactory @ Before public void sqlSessionFactory () throws Exception {InputStream inputStream = Resources.getResourceAsStream ("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder () .build (inputStream);} @ org.junit.Test public void helloWorld () {/ / sqlId = mapper.xml namespace.id String sqlId = "Student.selectAll"; List list = sqlSessionFactory.openSession (true) .selectList (sqlId); Assert.assertNotNull (list) Assert.assertFalse (list.isEmpty ()); log.info ("{}", list);} 5. Test result

After the above steps, we have completed the coding of mybatis hello world, which has been tested and run successfully. Some behaviors of mybatis can also be analyzed by observing the output log, and careful observation of the log is also a good way to learn. The complete code is under mybatis-demo in the Code Cloud https://gitee.com/kernelHP/java-demo directory. Select maven profile before compilation, as shown in the figure:

This is done to make it easier to test different mybatis versions. This demo uses jdk 1.8.If you don't have jdk 1.8 installed on your machine, you can download it here to https://jdk.java.net/.

At this point, the study on "MyBatis's method of writing hello world code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report