一.大配置
<?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"> <!--根节点,XMl只能有一个--> <configuration> <!--一个environments有N个environment--> <typeAliases> <package name="cn.bdqn.entity"></package> </typeAliases> <environments default="development"> <environment id="development"> <!-- transactionManager:JDBC保证事务的 update delete 事务分类:JDBC:编程式事务 xxx.beginTransaction() tx.commit() tx.rollback() 配置式事务 JDBC|MANAGED 区别 --> <transactionManager type="JDBC"></transactionManager> <!-- POOLED:MyBatis内置的连接池 c3p0连接池 POOLED 、UNPOOLED 、JNDI --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///blog"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/bdqn/dao/DeptMappr.xml"></mapper> </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: 命名空间:区分不同空间下的同名SQLID A: findlAll B: findAll --> <mapper namespace="cn.bdqn.dao.IDeptDao"> <!--SQL标签 id:唯一锁定到SQL标识 paramenterType:SQL语句的入参 可以省略 resultType: 增删除操作:不能 写 查询:单个实体的类型 --> <select id="getAll" resultType="Dept"> /*SQL文:SQL语句*/ SELECT * FROM bloginfo </select> <select id="getid" resultType="Dept"> /*SQL文:SQL语句*/ SELECT * FROM bloginfo WHERE blogId=#{blogId} </select> </mapper>
三.jar包
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <!--mysql jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
四.实体类
public class StudentInfo {
private Integer stuId;
private String stuName;
private Integer stuAge;
private Date stuDate;
}
五.更新POM.xml文件中build节点
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
六.测试类
InputStream is=Resources.getResourcesAsStream(path);
SessionFactory factory=new SessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
<?xml version="1.0" encoding="UTF-8" ?><!--文档类型,头文件--><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><!--根节点,XMl只能有一个--><configuration> <!--一个environments有N个environment--><typeAliases> <package name="cn.bdqn.entity"></package> </typeAliases> <environments default="development"> <environment id="development"> <!-- transactionManager:JDBC保证事务的 update delete 事务分类:JDBC:编程式事务 xxx.beginTransaction() tx.commit() tx.rollback() 配置式事务 JDBC|MANAGED 区别 --><transactionManager type="JDBC"></transactionManager> <!-- POOLED:MyBatis内置的连接池 c3p0连接池 POOLED 、UNPOOLED 、JNDI --><dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///blog"/> <property name="username" value="root"/> <property name="password" value="1234"/> </dataSource> </environment> </environments> <mappers><mapper resource="cn/bdqn/dao/DeptMappr.xml"></mapper> </mappers> </configuration>
时间: 2024-10-08 19:35:05