当前位置:   article > 正文

利用MyBatis实现查询所有数据_mybatis查询全部数据

mybatis查询全部数据

这操作真心记不住。。。。。还是写一遍加深记忆

1.首先创建user表,添加数据.

  1. create datebase fuck;
  2. use fuck;
  3. // 删除tb_brand表
  4. drop table if exists tb_brand;
  5. // 创建tb_brand表
  6. create table tb_brand
  7. (
  8. -- id 主键
  9. id int primary key auto_increment,
  10. -- 品牌名称
  11. brand_name varchar(20),
  12. -- 企业名称
  13. company_name varchar(20),
  14. -- 排序字段
  15. ordered int,
  16. -- 描述信息
  17. description varchar(100),
  18. -- 状态:0:禁用 1:启用
  19. status int
  20. );
  21. // 添加数据
  22. insert into tb_brand (brand_name, company_name, ordered, description, status)
  23. values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
  24. ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
  25. ('小米', '小米科技有限公司', 50, 'are you ok', 1);

2.创建Maven模块,导入坐标

导入坐标

  1. <dependencies>
  2. <!--mysql驱动-->
  3. <dependency>
  4. <groupId>mysql</groupId>
  5. <artifactId>mysql-connector-java</artifactId>
  6. <version>8.0.28</version>
  7. </dependency>
  8. <!--mybatis的依赖-->
  9. <dependency>
  10. <groupId>org.mybatis</groupId>
  11. <artifactId>mybatis</artifactId>
  12. <version>3.5.5</version>
  13. </dependency>
  14. <!--junit单元测试-->
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.12</version>
  19. <scope>test</scope>
  20. </dependency>
  21. <!-- 添加slf4j日志api -->
  22. <dependency>
  23. <groupId>org.slf4j</groupId>
  24. <artifactId>slf4j-api</artifactId>
  25. <version>1.7.20</version>
  26. </dependency>
  27. <!-- 添加logback-classic依赖 -->
  28. <dependency>
  29. <groupId>ch.qos.logback</groupId>
  30. <artifactId>logback-classic</artifactId>
  31. <version>1.2.3</version>
  32. </dependency>
  33. <!-- 添加logback-core依赖 -->
  34. <dependency>
  35. <groupId>ch.qos.logback</groupId>
  36. <artifactId>logback-core</artifactId>
  37. <version>1.2.3</version>
  38. </dependency>
  39. </dependencies>

 3.编写MyBatis的核心配置文件,并且导入logback.xml文件

(1)mybatis配置文件

并且把相应的数据库信息导入进去

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <!--起别名-->
  7. <typeAliases>
  8. <package name="com.fuck.pojo"/>
  9. </typeAliases>
  10. <environments default="development">
  11. <environment id="development">
  12. <transactionManager type="JDBC"/>
  13. <dataSource type="POOLED">
  14. <!--数据库连接信息-->
  15. <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
  16. <property name="url" value="jdbc:mysql:///fuck?useSSL=false&amp;useServerPrepStmts=true"/>
  17. <property name="username" value="root"/>
  18. <property name="password" value="123456"/>
  19. </dataSource>
  20. </environment>
  21. </environments>
  22. <mappers>
  23. <!--扫描mapper-->
  24. <package name="com.fuck.mapper"/>
  25. </mappers>
  26. </configuration>

(2) logback.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <!--
  4. CONSOLE :表示当前的日志信息是可以输出到控制台的。
  5. -->
  6. <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
  7. <encoder>
  8. <pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %n</pattern>
  9. </encoder>
  10. </appender>
  11. <logger name="com.fuck" level="DEBUG" additivity="false">
  12. <appender-ref ref="Console"/>
  13. </logger>
  14. <!--
  15. level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF
  16. , 默认debug
  17. <root>可以包含零个或多个<appender-ref>元素,标识这个输出位置将会被本日志级别控制。
  18. -->
  19. <root level="DEBUG">
  20. <appender-ref ref="Console"/>
  21. </root>
  22. </configuration>

4.编写pojo实体类,用get 、set方法

  1. package com.fuck.pojo;
  2. public class Brand {
  3. private Integer id;
  4. private String brandName;
  5. private String companyName;
  6. private Integer ordered;
  7. private String description;
  8. private Integer status;
  9. public Integer getId() {
  10. return id;
  11. }
  12. public void setId(Integer id) {
  13. this.id = id;
  14. }
  15. public String getBrandName() {
  16. return brandName;
  17. }
  18. public void setBrandName(String brandName) {
  19. this.brandName = brandName;
  20. }
  21. public String getCompanyName() {
  22. return companyName;
  23. }
  24. public void setCompanyName(String companyName) {
  25. this.companyName = companyName;
  26. }
  27. public Integer getOrdered() {
  28. return ordered;
  29. }
  30. public void setOrdered(Integer ordered) {
  31. this.ordered = ordered;
  32. }
  33. public String getDescription() {
  34. return description;
  35. }
  36. public void setDescription(String description) {
  37. this.description = description;
  38. }
  39. public Integer getStatus() {
  40. return status;
  41. }
  42. public void setStatus(Integer status) {
  43. this.status = status;
  44. }
  45. @Override
  46. public String toString() {
  47. return "Brand{" +
  48. "id=" + id +
  49. ", brandName='" + brandName + '\'' +
  50. ", companyName='" + companyName + '\'' +
  51. ", ordered=" + ordered +
  52. ", description='" + description + '\'' +
  53. ", status=" + status +
  54. '}';
  55. }
  56. }

5.创建mapper接口

  1. package com.fuck.mapper;
  2. import com.fuck.pojo.Brand;
  3. import java.util.List;
  4. public interface BrandMapper {
  5. //查询所有
  6. public List<Brand> selectAll();
  7. }

6.创建SqlSessionFactory工具类

  1. package com.fuck.Utils;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. public class SqLSessionFactoryUtils {
  8. private static SqlSessionFactory sqlSessionFactory;
  9. static {
  10. String resource = "mybatis-config.xml";
  11. InputStream inputStream = null;
  12. try {
  13. inputStream = Resources.getResourceAsStream(resource);
  14. } catch (IOException e) {
  15. throw new RuntimeException(e);
  16. }
  17. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  18. }
  19. public static SqlSessionFactory getSqlSessionFactory(){
  20. return sqlSessionFactory;
  21. }
  22. }

总的项目结构

7.创建Mapper映射文件,并写入sql查询语句

BrandMapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <!--namespace名称空间-->
  6. <mapper namespace="com.fuck.mapper.BrandMapper">
  7. <!--resultMap:
  8. 1.定义resultMap标签
  9. 2.在<select>标签中,使用resultMap属性替换resultType属性
  10. -->
  11. <resultMap id="brandResultMap" type="brand">
  12. <!-- id:完成主键字段映射-->
  13. <result column="brand_name" property="brandName"></result>
  14. <result column="company_name" property="companyName"></result>
  15. </resultMap>
  16. <!--查询所有-->
  17. <select id="selectAll" resultMap="brandResultMap">
  18. select * from tb_brand;
  19. </select>
  20. </mapper>

8. 最后编写测试类

MyBatisTest

  1. package com.fuck.test;
  2. import com.fuck.Utils.SqLSessionFactoryUtils;
  3. import com.fuck.mapper.BrandMapper;
  4. import com.fuck.pojo.Brand;
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7. import org.junit.Test;
  8. import java.util.Base64;
  9. import java.util.List;
  10. public class MyBatisTest {
  11. // 调用SqlSessionFactory方法 构建出SqlSessionFactory的实例 factory
  12. SqlSessionFactory factory= SqLSessionFactoryUtils.getSqlSessionFactory();
  13. // 利用注解
  14. @Test
  15. public void testSelectAll(){
  16. // 获取SqlSession
  17. SqlSession sqlSession= factory.openSession();
  18. // 获取BrandMapper
  19. BrandMapper brandmapper=sqlSession.getMapper(BrandMapper.class);
  20. // 调用方法
  21. List<Brand> brands=brandmapper.selectAll();
  22. // 输出
  23. System.out.println(brands);
  24. // 关闭sqlSession
  25. sqlSession.close();
  26. }
  27. }

 运行结果

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/569369
推荐阅读
相关标签
  

闽ICP备14008679号