java——mybatis随笔 迷南。 2024-02-25 01:31 67阅读 0赞 教程:https://www.cnblogs.com/xiaobaibailongma/p/17019484.html 本地示例:https://www.cnblogs.com/xiaobaibailongma/p/17019676.html ========================================================================= gitee:示例 spring\_boot/pom.xml 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/pom.xml spring\_boot/src/main/resources/application.properties 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/resources/application.properties spring\_boot/src/main/resources/mappers/MyUserMapper.xml 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/resources/mappers/MyUserMapper.xml spring\_boot/src/main/java/org/example/mapper/UserMapper.java 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/java/org/example/mapper/UserMapper.java spring\_boot/src/main/java/org/example/service/UserService.java 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/java/org/example/service/UserService.java spring\_boot/src/main/java/org/example/service/Impl/UserServiceImpl.java 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/java/org/example/service/Impl/UserServiceImpl.java spring\_boot/src/main/java/org/example/controller/mybatis/MyUserController.java 配置文件:https://gitee.com/xiaobailongbailongma/springboot/blob/master/spring\_boot/src/main/java/org/example/controller/mybatis/MyUserController.java ======================================================================= 总体步骤按照如下: ![b9c662ee03f6c130d9523a231ca75394.png][] 第二步——修改pom文件: ![复制代码][de765b934d6f0bcdcd403663a32a32a6.gif] <?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project> ![复制代码][e953fb0f6309a84ead6d9ef4c2570a7f.gif] ![43f75daed213077cf2954f3cf2efc70b.png][] ![95fe2038b85508c51184862f4b92a52b.png][] 也可以按照教程上面的写,格式如下: ![复制代码][e22ae6733a89e29034b9a6a0e100e3b2.gif] <?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>spring_boot</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project> ![复制代码][9f4ac1438e2f110c9482ce37f137e2a0.gif] ![3b45bb749ea769ef118f3785c9d98c22.png][] ![a94bd85f58a1524015832f6db97139af.png][] 第二步——配置数据源: application.properties: ![复制代码][fe6293afe1bbbcc4ccdc9c5c3805bbe0.gif] spring.main.banner-mode = off test.environment = read config by environment obj.sname=chenheng obj.sage=88 #设定日志的默认级别为info logging.level.root=info ##设定org包下的日志级别为warn #logging.level.org=warn ##设定com.ch.ch4_1包下的日志级别为debug #logging.level.com.ch.ch4_1=debug logging.file=c:/log/my.log logging.pattern.console=%level %date{yyyy-MM-dd HH:mm:ss:SSS} %logger{50}.%M %L :%m%n logging.pattern.file=%level %date{ISO8601} %logger{50}.%M %L :%m%n ### ##数据源信息配置 ### #数据库地址 spring.datasource.url=jdbc:mysql://localhost:3306/mysql8?characterEncoding=utf8 #数据库用户名 spring.datasource.username=root #数据库密码 spring.datasource.password=123456 #数据库驱动 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #告诉系统在哪里去找mapper.xml文件(映射文件) mybatis.mapperLocations=classpath:mappers/*.xml ![复制代码][1eb290d72e198d844df59cbe358e7604.gif] ![601a3665b084e2b0a1c668252389bd33.png][] 第四步——创建实体类: ![复制代码][ab6ed726148401bb8d1ee0cdd24b76d5.gif] package org.example.entity; public class MyUser { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ![复制代码][95adb858a24af670d8509a20748644ec.gif] ![8cc3185f102267dc94112104cd0f9014.png][] 第五步——创建数据库访问接口: ![复制代码][3dace49446fa9dcabce091af0509fef8.gif] package org.example.mapper; import org.example.entity.MyUser; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { public List<MyUser> findAll(); } ![复制代码][d348f53172364809ab8d7ad6fff3d83f.gif] ![9b2d78fb77beb7da1fadd9437a182b7a.png][] 第六步——创建mapper映射文件: ![复制代码][f804883582232841eed6699416ad9640.gif] <?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.example.mapper.UserMapper"> <select id="findAll" resultType="org.example.entity.MyUser"> select * from users </select> </mapper> ![复制代码][8f4f5354a7d0a790ca5d3999944b42d7.gif] ![109141794db7ec34ecd0e6fa4e7e939d.png][] 第七步——创建业务层: ![复制代码][949566e3c69d7b7014b383c1c55c85d3.gif] package org.example.service; import org.example.entity.MyUser; import java.util.List; public interface UserService { List<MyUser> findAll(); } ![复制代码][c9f3f9887ee77f9f0af76e3c23296a0d.gif] ![a3923844783ff4e669a700e830802ae8.png][] ![复制代码][5aab4bc96831a364effcd9d3a38594ed.gif] package org.example.service.Impl; import org.example.entity.MyUser; import org.example.mapper.UserMapper; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired UserMapper userMapper; @Override public List<MyUser> findAll() { return userMapper.findAll(); } } ![复制代码][390f0b58b0c94e297130d97e8307db1f.gif] ![5c3473cdf25f580cfefbe8dc093a4040.png][] 第八步——创建控制器类: ![复制代码][d4accc50d144ecbde1ac36bc3c75fe69.gif] package org.example.controller; import java.util.List; import org.example.entity.MyUser; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyUserController { @Autowired private UserService userService; @GetMapping(value = "/1") public List<MyUser> home1() { // 查询所有网站 List<MyUser> users = userService.findAll(); for (MyUser user : users) { System.out.println(user.getId()+" "+user.getName()+" "+user.getName()); } return users; } } ![复制代码][19c9ce93254392ec321adc3f18d45900.gif] ![cc634e03635207a4fdc1e1ca1374815c.png][] 第九步——运行主类: ![复制代码][f3a93ef5fa11f76281f4e6b97dfc4670.gif] package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class helloWorldApplication { public static void main(String[] args) { SpringApplication.run(helloWorldApplication.class, args); } } ![复制代码][711d79d9989146af4a78fd5ea944d803.gif] ![fac173603260a6784dca27e55facdef9.png][] 第十步——访问: ![d79d71b57a82ad5c6511372644423963.png][] ![a1af8f06225aa8bcc0239cf8fda25065.png][] ![fe68ecb82f85b77355ab44243f9afc46.png][] 数据库表信息: ![复制代码][f66f3c95500ce8edd6e4421f7c03bae8.gif] CREATE TABLE `users` ( `id` int NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; ![复制代码][2a7b614afd00b39d5fb6e6086d302a8c.gif] ![33e3b58e195b8d22fb0f787fe73a885a.png][] [b9c662ee03f6c130d9523a231ca75394.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/d78b73e6bddb4b1c91660793b88d52ca.png [de765b934d6f0bcdcd403663a32a32a6.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/9ced19f790694826b1b7fdd72b094720.png [e953fb0f6309a84ead6d9ef4c2570a7f.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/62b6bcab6afa4e4ab7f38b8300dffc7a.png [43f75daed213077cf2954f3cf2efc70b.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/fdb1b5ea1fdb46c1a7873b37b30882d4.png [95fe2038b85508c51184862f4b92a52b.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/0034bbe63c9c4f248f37a19b1f5aa7ab.png [e22ae6733a89e29034b9a6a0e100e3b2.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/71afb3ad3af344cc8f97c940eef6007b.png [9f4ac1438e2f110c9482ce37f137e2a0.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/3da0981912b74a3db3abe4650abe7ec2.png [3b45bb749ea769ef118f3785c9d98c22.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/7df57be728044717851b4e60cf91ef9e.png [a94bd85f58a1524015832f6db97139af.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/ec773e41c9244a14a3285a83f384f662.png [fe6293afe1bbbcc4ccdc9c5c3805bbe0.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/836d6b30e67d4015b66053122592c809.png [1eb290d72e198d844df59cbe358e7604.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/a854abe728d4464297ff08dc94b3d021.png [601a3665b084e2b0a1c668252389bd33.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/9aa6b354d8fd4c98bf5ab3eec8be404c.png [ab6ed726148401bb8d1ee0cdd24b76d5.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/7a528d10dac84b12af5803fcf41065bf.png [95adb858a24af670d8509a20748644ec.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/099b80844b4c481eb86b32486fe1af21.png [8cc3185f102267dc94112104cd0f9014.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/5776cf020ff54ec7a37d7f1ec0d3e544.png [3dace49446fa9dcabce091af0509fef8.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/d6caac0990da4c298672891fe993ffdb.png [d348f53172364809ab8d7ad6fff3d83f.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/f7bf6e4b7c4b4c9ea58e40dca09457a8.png [9b2d78fb77beb7da1fadd9437a182b7a.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/d0122977ac2b4d1f95bcc3edeb9245c3.png [f804883582232841eed6699416ad9640.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/a68f172166474bd38c5fe2ef3cc73173.png [8f4f5354a7d0a790ca5d3999944b42d7.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/6fa3488e9d414bb78fbd296d010c1751.png [109141794db7ec34ecd0e6fa4e7e939d.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/3f0abafa4a6a4304a50dbc5634de5bad.png [949566e3c69d7b7014b383c1c55c85d3.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/3b6f961687994c07933ecb4d187a3333.png [c9f3f9887ee77f9f0af76e3c23296a0d.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/5e325d070b3045c4a36fd895eeb011d5.png [a3923844783ff4e669a700e830802ae8.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/720832956eb342828b48119e2996ef7e.png [5aab4bc96831a364effcd9d3a38594ed.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/7ef342719dcb49d6bc609098ba674341.png [390f0b58b0c94e297130d97e8307db1f.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/69465e1510b44ef7ab3b533a4830581f.png [5c3473cdf25f580cfefbe8dc093a4040.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/7089722eec9c42fba9131239244f94ca.png [d4accc50d144ecbde1ac36bc3c75fe69.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/c50e2b2c1dcc4b5283d9c84c51ace726.png [19c9ce93254392ec321adc3f18d45900.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/e955091e857045aabfac27f6ab7f7d5a.png [cc634e03635207a4fdc1e1ca1374815c.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/4d9a7c1700b0480485c1842faf197399.png [f3a93ef5fa11f76281f4e6b97dfc4670.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/cb61e6dfe8c245deb42b1dd4e2e122e0.png [711d79d9989146af4a78fd5ea944d803.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/0586f2d3aabf4f4a9dc3f6e44662db4d.png [fac173603260a6784dca27e55facdef9.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/121eec741797490a8a23de83553b30ec.png [d79d71b57a82ad5c6511372644423963.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/d4447b7a57e04691af6e10a814552d40.png [a1af8f06225aa8bcc0239cf8fda25065.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/c782375531e04917b036f56c848ac530.png [fe68ecb82f85b77355ab44243f9afc46.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/d5068884cf8d4df082583b7d7e3d12d2.png [f66f3c95500ce8edd6e4421f7c03bae8.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/9ff3b5e86b5b422c9bca99731debb754.png [2a7b614afd00b39d5fb6e6086d302a8c.gif]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/eeb75138d74b4b0bb792e7bcfde08804.png [33e3b58e195b8d22fb0f787fe73a885a.png]: https://image.dandelioncloud.cn/pgy_files/images/2024/02/25/63fb445b405c47bdbe5c6bd31ee9c724.png
相关 随笔 这几天一直在做停车场项目,今天没怎么做题目,就随便看了一题练练手。 题目:查找最小的k个元素(笔试题) 输入n个整数,输出其中最小的k个。例如输入1,2,3,4,5,6 缺乏、安全感/ 2022年07月14日 15:23/ 0 赞/ 262 阅读
相关 随笔 今天是2017年12月2日,星期六,我用来一下午的时间把自己的导航改名为山理人导航,把所有的影视资源都整合了一下,也算是娱乐了一下,接下来的时间可能不太经常写博客了,要专心考研 偏执的太偏执、/ 2022年06月04日 02:54/ 0 赞/ 272 阅读
相关 随笔 本来想写完23种设计模式在学习点新技术呢。但是年底了,年后辞职高峰季,朕也有点动摇了。 奈何工资太低,穷到吃土。所以暂时决定接下来写写面试常常问到的框架,缓存,数据库的东 客官°小女子只卖身不卖艺/ 2022年05月31日 04:24/ 0 赞/ 258 阅读
相关 随笔 \ update select 语句(注意:必须使用inner join) \ 语法 update a inner join (select yy from b) c on 你的名字/ 2022年05月15日 11:59/ 0 赞/ 283 阅读
相关 随笔 人的心中应该有种信念,他支撑你活下去的意念。在你苦的时候,你会坚强的面对;在你高兴的时候,你会平淡的接受。 这个世界只有父母的爱是无私的,所以你应该珍惜别人的爱,更 - 日理万妓/ 2022年04月14日 02:45/ 0 赞/ 307 阅读
相关 随笔 坐在图书馆,看着一天太阳升起又落下 总想写点什么,又无处落笔 越学习越觉得自己无知,这是一句实话 考试临近 实习未找 秋招将至 好想回到大一逼自己一把 港控/mmm°/ 2022年01月14日 09:39/ 0 赞/ 311 阅读
相关 随笔 今天,经理让我说某程序思路,还没写非得让我把思路全都说出来。不评价了!他确实比我强很多,从这方面讲我没话说。 说说今天的收获,如果数据库中有上千个表的数据,你有30w 心已赠人/ 2021年12月22日 08:07/ 0 赞/ 411 阅读
相关 随笔 Assert.hasText Spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率、增强代码质量。下面就最常用 朴灿烈づ我的快乐病毒、/ 2021年12月18日 15:17/ 0 赞/ 397 阅读
相关 随笔 马上进入6月,本学期学习也将结束 关于学习Python的体会:这确实是一门简练的语言,不需要繁琐的’;’、‘\{\}’、只需要注意缩进格式,省事, 小咪咪/ 2021年12月16日 14:29/ 0 赞/ 391 阅读
相关 随笔 看了几篇设计模式方面的文章,觉得有点空,体会并不是很深,我想在下一个系统中多多考虑一下这些方面,也许会体会深刻些。 今天在这个blog逛了好久,发现有些这位高手自己写的工具, 青旅半醒/ 2021年09月10日 03:48/ 0 赞/ 441 阅读
还没有评论,来说两句吧...