MyBatis基本运行环境
拷贝jar加入到项目中build path jar包
创建数据库的表及数据添加
USE [mybatis] CREATE TABLE [dbo].[Material]( [id] [int] IDENTITY(1,1) NOT NULL, [name] [varchar](50) NULL, [price] [int] NULL )
创建项目
创建实体类
package com.ahabest.bean; public class Material { //成员变量 private Integer id; private String name; private Integer price; // public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } @Override public String toString() { return "Material [id=" + id + ", name=" + name + ", price=" + price + "]"; } public Material() { super(); } public Material(Integer id, String name, Integer price) { super(); this.id = id; this.name = name; this.price = price; } }
创建接口及映射
package com.ahabest.dao; import java.util.List; import com.ahabest.bean.Material; public interface MaterialDao { List<Material> getAll(); }
<?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="test"> <!-- id是找到SQL语句的唯一标识 --> <!-- parameter表示传入的参数类型 --> <!-- resultType返回值的类型 --> <select id="getAll" resultType="com.ahabest.bean.Material"> SELECT * FROM Material </select> <select id="getById" parameterType="java.lang.Integer" resultType="com.ahabest.bean.Material"> SELECT * FROM Material WHERE id = #{id} </select> </mapper>
创建数据库配置及映射
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=mybatis jdbc.username=sa jdbc.password=
<?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> <!-- 加载数据库的配置信息 --> <properties resource="jdbc.properties"></properties> <!-- mybatis的数据库环境 --> <environments default="Enviroment"> <environment id="Enviroment"> <!-- 事务管理 --> <transactionManager type="JDBC"></transactionManager> <!-- 数据源 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ahabest/dao/MaterialMapper.xml"/> </mappers> </configuration>
读取数据库配置
package com.ahabest.dao.impl; import java.io.InputStream; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.ahabest.bean.Material; import com.ahabest.dao.MaterialDao; public class MaterialDaoImpl implements MaterialDao { @Override public List<Material> getAll() { // 读取配置文件 InputStream is = MaterialDaoImpl.class.getClassLoader().getResourceAsStream("mybatis-config.xml"); //创建会话工厂 SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is); //开启会话 SqlSession ss =ssf.openSession(); //调用方法 List<Material> lm = ss.selectList("test.getAll"); Material m = ss.selectOne("test.getById",1); System.out.println(m); return lm; } }
测试
package com.ahabest.test; import java.util.List; import com.ahabest.bean.Material; import com.ahabest.dao.MaterialDao; import com.ahabest.dao.impl.MaterialDaoImpl; public class MaterialDaoTest { public static void main(String[] args) { MaterialDao md = new MaterialDaoImpl(); List<Material> lm = md.getAll(); for(Material m:lm) { System.out.println(m); } } }