您现在的位置是:首页 > 个人日记个人日记
mybatis-01-快速上手
2019-02-02 13:16:58【个人日记】725人已围观
简介这是我学习mybatis的第一天,这也是比较简单的,跟我一起看看吧
mybatis快速上手
配置pom.xml
文件
<?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>test</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!-- mybatis.jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- junit.jar -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置mybatis-config.xml
文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<!-- 使用JDBC技术 -->
<transactionManager type="JDBC"/>
<!-- 数据库连接池,mybatis连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mytest"/>
<property name="username" value="huangxin"/>
<property name="password" value="1538933906"/>
</dataSource>
</environment>
</environments>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="ProductCaregoryMapper.xml"/>
</mappers>
</configuration>
配置 ProductCaregoryMapper.xml
文件
<?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">
<!-- Sql语句 -->
<mapper namespace="category">
<select id="fingAll" resultType="com.huangxin.model.ProductCategory">
SELECT * FROM product
</select>
</mapper>
模型对象
Product.java
package com.huangxin.model;
import java.math.BigDecimal;
public class ProductCategory {
private Long id;
private String productName;
private BigDecimal salePrice;
private String supplier;
private String brand;
private BigDecimal costPrice;
@Override
public String toString() {
return "ProductCategory{" +
"id=" + id +
", productName='" + productName + '\'' +
", salePrice=" + salePrice +
", supplier='" + supplier + '\'' +
", brand='" + brand + '\'' +
", costPrice=" + costPrice +
'}';
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public BigDecimal getSalePrice() {
return salePrice;
}
public void setSalePrice(BigDecimal salePrice) {
this.salePrice = salePrice;
}
public String getSupplier() {
return supplier;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public BigDecimal getCostPrice() {
return costPrice;
}
public void setCostPrice(BigDecimal costPrice) {
this.costPrice = costPrice;
}
}
SqlSessionTest.java
package com.huangxin.product;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class SqlSessionTest {
private SqlSessionFactory sqlSessionFactory;
/**
* 初始化SqlSession工厂
* @throws IOException
*/
@Before
public void init() throws IOException {
//将全局配置文件读取
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void test(){
// 获取一个sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
List<Object> objects = sqlSession.selectList("category.fingAll");
for (Object object : objects) {
System.out.println(object);
}
}
}
输出结果为:
Tags: JavaWeb
上一篇: mybatis-02-CRUD简单操作
相关文章
随机图文
评论区
2024-12-21 23:30:45
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信