mybatis入门demo01
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> <!-- 指定数据源环境,default指的是使用哪个数据源 --> <environments default="development"> <!-- 定义数据源的信息 --> <environment id="development"> <!-- type="JDBC" 表示事务由处理jdbc处理,type="MANAGED"表示事务由容器来管理 --> <transactionManager type="JDBC" /> <!-- type="POOLED"表示使用连接池 type="UNPOOLED"表示不使用连接池 --> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://120.77.40.157:3306/mybatis?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 加载类的映射文件 --> <mappers> <mapper resource="com/jove/domain/DeptMapper.xml" /> </mappers> </configuration>
实体类:
package com.jove.domain;
import java.io.Serializable;
public class Dept implements Serializable {
private static final long serialVersionUID = 1L;
private Integer deptId; //部门编号
private String deptName;//部门名称
private String deptAddress;//部门地址
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptAddress() {
return deptAddress;
}
public void setDeptAddress(String deptAddress) {
this.deptAddress = deptAddress;
}
@Override
public String toString() {
return "Dept [deptId=" + deptId + ", deptName=" + deptName
+ ", deptAddress=" + deptAddress + "]";
}
}
实体映射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"> <!-- 命名空间可以任选命名,但最好要定义一定规则,便于后继的使用 --> <mapper namespace="com.jove.domain.deptMapper"> <!-- 一般在查询时使用 --> <resultMap type="com.jove.domain.Dept" id="deptResultMap"> <!-- id用来配置表的主键与类的属性的映射关系 column指定表的字段名 property指定类的属性名 result指定表的非主键与类的映射关系 --> <id column="dept_id" property="deptId" /> <result column="dept_name" property="deptName" /> <result column="dept_address" property="deptAddress" /> </resultMap> <!-- 定义插入的sql语句,通过命名空间+id方式被定位 --> <insert id="insertDept" parameterType="com.jove.domain.Dept"> <!-- #{}用来获取传过来的参数 --> insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress}); </insert> </mapper>
DaoImpl:
package com.jove.dao.impl;
import java.io.IOException;
import java.io.Reader;
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 com.jove.domain.Dept;
public class DeptDaoImpl {
private SqlSessionFactory sqlSessionFactory;
private SqlSession session;
private int insert;
private Reader reader;
/**
* 1.read configuration 2.new sqlSessionFactory 3.new session 4.start
* transaction 5.handle data 6.commit or rollback transaction 7.close
* session
*/
public int save(Dept dept) {
try {
reader = Resources.getResourceAsReader("Mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
session = sqlSessionFactory.openSession();
insert = session.insert("com.jove.domain.deptMapper.insertDept", dept);
session.commit();
} catch (IOException e) {
session.rollback();
e.printStackTrace();
}finally {
if(session!=null){
session.close();
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return insert;
}
}
JunitTest:
package com.jove.dao.impl;
import org.junit.Before;
import org.junit.Test;
import com.jove.domain.Dept;
public class DeptDaoImplTest {
private DeptDaoImpl deptdao;
@Before
public void setup() {
deptdao = new DeptDaoImpl();
}
@Test
public void save() {
Dept dept = new Dept();
dept.setDeptName("生活部");
dept.setDeptAddress("深圳");
deptdao.save(dept);
}
}
注意:
parameterType指定输入参数的java类型,可以填写别名或Java类的全限定名。
resultType指定输出结果的java类型,可以填写别名或Java类的全限定名。
selectOne:只能查询0或1条记录,大于1条记录的话,会报错
selectList:可以查询0或N条记录
#{}:相当于预处理中的占位符?。
#{}里面的参数表示接收java输入参数的名称。
#{}可以接受HashMap、简单类型、POJO类型的参数。
当接受简单类型的参数时,#{}里面可以是value,也可以是其他。
#{}可以防止SQL注入。
${}:相当于拼接SQL串,对传入的值不做任何解释的原样输出。
${}会引起SQL注入,所以要谨慎使用。
${}可以接受HashMap、简单类型、POJO类型的参数。
当接受简单类型的参数时,${}里面只能是value。
浙公网安备 33010602011771号