核心配置文件mybatis
<?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>
<typeAliases>
<package name="com.itheima.pojo"/>
</typeAliases>
<!--
environments:配置数据库连接环境信息。可以配置多个environment,通过default属性切换不同的environment
-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接信息-->
<!--固定写法-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--指定数据库-->
<property name="url" value="jdbc:mysql:///db1?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库连接信息-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db1?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--加载sql映射文件-->
<!-- <mapper resource="com/itheima/mapper/UserMapper.xml"/>-->
<!--<mapper resource="demo2/StuMapper.xml"/>-->
<!--Mapper代理方式-->
<!--指定文件夹下所有配置文件都可以读取-->
<package name="demo2"/>
</mappers>
</configuration>
映射配置文件
<?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">
<!--
namespace:名称空间
-->
<mapper namespace="demo2.MallMapper">
<!--配置的方法,通过接口和映射可以使用这个sql语句操作数据库-->
<!--resultType规定结果的类型和指定类里面的类型一直-->
<select id="selectAll" resultType="pojo.Mall">
select * from product ;
</select>
</mapper>
实现类
package demo;
import demo2.MallMapper;
import demo2.StuMapper;
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 pojo.Mall;
import pojo.Stu;
import java.io.InputStream;
import java.util.List;
public class MybatisDemo3 {
public static void main(String[] args) throws Exception {
// 读取核心配置文件中的数据,转换成io流
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
// 创建一个SqlSessionFactory对象,通过SqlSessionFactoryBuilder里面的方法(参数是核心配置文件的io流)
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
// 使用里面的方法开启Session,用来执行sql语句
SqlSession sqlSession = build.openSession();
// 获取Mapper接口的代理对象
MallMapper mapper = sqlSession.getMapper(MallMapper.class);
// 调用接口中写的方法
List<Mall> malls = mapper.selectAll();
for (Mall mall : malls) {
System.out.println(mall);
}
sqlSession.close();
}
}
接口
package demo2;
import pojo.Mall;
import java.util.List;
public interface MallMapper {
// 接口的抽象方法名和映射文件中的方法id要一致
List<Mall> selectAll();
}
实体类
package pojo;
public class Mall {
private Integer id;
private String pname;
private Double price;
private Integer count;
private String description;
public Mall() {
}
public Mall(Integer id, String pname, Double price, Integer count, String description) {
this.id = id;
this.pname = pname;
this.price = price;
this.count = count;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Mall{" +
"id=" + id +
", pname='" + pname + '\'' +
", price=" + price +
", count=" + count +
", description='" + description + '\'' +
'}';
}
}