您现在的位置是:首页 > 个人日记个人日记

mybatis-01-快速上手

2019-02-02 13:16:58【个人日记】112人已围观

简介这是我学习mybatis的第一天,这也是比较简单的,跟我一起看看吧

mybatis快速上手


配置pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>test</groupId>
  7. <artifactId>mybatis</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- MySQL驱动 -->
  10. <dependency>
  11. <groupId>mysql</groupId>
  12. <artifactId>mysql-connector-java</artifactId>
  13. <version>5.1.6</version>
  14. </dependency>
  15. <!-- mybatis.jar -->
  16. <dependency>
  17. <groupId>org.mybatis</groupId>
  18. <artifactId>mybatis</artifactId>
  19. <version>3.4.6</version>
  20. </dependency>
  21. <!-- junit.jar -->
  22. <dependency>
  23. <groupId>junit</groupId>
  24. <artifactId>junit</artifactId>
  25. <version>4.12</version>
  26. <scope>test</scope>
  27. </dependency>
  28. </dependencies>
  29. </project>

配置mybatis-config.xml文件

  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. <environments default="development">
  7. <environment id="development">
  8. <!-- 使用JDBC技术 -->
  9. <transactionManager type="JDBC"/>
  10. <!-- 数据库连接池,mybatis连接池 -->
  11. <dataSource type="POOLED">
  12. <property name="driver" value="com.mysql.jdbc.Driver"/>
  13. <property name="url" value="jdbc:mysql://localhost:3306/mytest"/>
  14. <property name="username" value="huangxin"/>
  15. <property name="password" value="1538933906"/>
  16. </dataSource>
  17. </environment>
  18. </environments>
  19. <!-- 配置映射文件 -->
  20. <mappers>
  21. <mapper resource="ProductCaregoryMapper.xml"/>
  22. </mappers>
  23. </configuration>

配置 ProductCaregoryMapper.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. <!-- Sql语句 -->
  6. <mapper namespace="category">
  7. <select id="fingAll" resultType="com.huangxin.model.ProductCategory">
  8. SELECT * FROM product
  9. </select>
  10. </mapper>

模型对象

Product.java

  1. package com.huangxin.model;
  2. import java.math.BigDecimal;
  3. public class ProductCategory {
  4. private Long id;
  5. private String productName;
  6. private BigDecimal salePrice;
  7. private String supplier;
  8. private String brand;
  9. private BigDecimal costPrice;
  10. @Override
  11. public String toString() {
  12. return "ProductCategory{" +
  13. "id=" + id +
  14. ", productName='" + productName + '\'' +
  15. ", salePrice=" + salePrice +
  16. ", supplier='" + supplier + '\'' +
  17. ", brand='" + brand + '\'' +
  18. ", costPrice=" + costPrice +
  19. '}';
  20. }
  21. public Long getId() {
  22. return id;
  23. }
  24. public void setId(Long id) {
  25. this.id = id;
  26. }
  27. public String getProductName() {
  28. return productName;
  29. }
  30. public void setProductName(String productName) {
  31. this.productName = productName;
  32. }
  33. public BigDecimal getSalePrice() {
  34. return salePrice;
  35. }
  36. public void setSalePrice(BigDecimal salePrice) {
  37. this.salePrice = salePrice;
  38. }
  39. public String getSupplier() {
  40. return supplier;
  41. }
  42. public void setSupplier(String supplier) {
  43. this.supplier = supplier;
  44. }
  45. public String getBrand() {
  46. return brand;
  47. }
  48. public void setBrand(String brand) {
  49. this.brand = brand;
  50. }
  51. public BigDecimal getCostPrice() {
  52. return costPrice;
  53. }
  54. public void setCostPrice(BigDecimal costPrice) {
  55. this.costPrice = costPrice;
  56. }
  57. }

SqlSessionTest.java

  1. package com.huangxin.product;
  2. import org.apache.ibatis.io.Resources;
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.List;
  11. public class SqlSessionTest {
  12. private SqlSessionFactory sqlSessionFactory;
  13. /**
  14. * 初始化SqlSession工厂
  15. * @throws IOException
  16. */
  17. @Before
  18. public void init() throws IOException {
  19. //将全局配置文件读取
  20. InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
  21. sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
  22. }
  23. @Test
  24. public void test(){
  25. // 获取一个sqlSession
  26. SqlSession sqlSession = sqlSessionFactory.openSession();
  27. List<Object> objects = sqlSession.selectList("category.fingAll");
  28. for (Object object : objects) {
  29. System.out.println(object);
  30. }
  31. }
  32. }

输出结果为:

控制台输出

Tags: JavaWeb  

评论区

    2019-07-24 13:31:44

    站长

    欢迎各位评论!


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我