本节介绍MyBatis的使用方法(没有和spring相结合,仅仅是mybatis的使用方法)
1、引入jar包
mybatis包(mybatis-3.5.0.jar)
mysql驱动包(mysql-connector-java-5.1.48-bin)
这两个jar包没有可以去官网下载
2、src目录下新建SqlMapConfig.xml
SqlMapConfig.xml用于指定数据库连接参数和SQL定义文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"/>
<property name="username" value="root" />
<property name="password" value="asdf123456" />
</dataSource>
</environment>
</environments>
<!-- 加载SQL定义文件 -->
<mappers>
<mapper resource="cn/springmybatis/entity/EmpMapper.xml" />
</mappers>
</configuration>
3、实体类创建
Emp实体类创建,主要用于返回值类型
package cn.springmybatis.entity;
import java.io.Serializable;
public class Emp implements Serializable{
public Emp() {
}
public Emp(String name, Double salary) {
super();
this.name = name;
this.salary = salary;
}
public Emp(String name, Integer sex, Double salary) {
super();
this.name = name;
this.sex = sex;
this.salary = salary;
}
private Integer id;
private String name;
private Integer sex;
private Double salary;
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 getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
4、创建sql.xml文件
创建EmpMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<!--namespace的值全局唯一-->
<mapper namespace="empsql">
<select id="findAll" resultType="cn.springmybatis.entity.Emp">
select * from emp
</select>
<select id="findBySex" parameterType="int" resultType="cn.springmybatis.entity.Emp" >
select * from emp where sex=#{sex}
</select>
<select id="findById" parameterType="int" resultType="cn.springmybatis.entity.Emp">
select * from emp where id=#{a}
</select>
<insert id="insert" parameterType="cn.springmybatis.entity.Emp">
insert into emp (name,sex,salary) values(#{name},#{sex},#{salary})
</insert>
<delete id="deleteById" parameterType="int">
delete from emp where id=#{id}
</delete>
<update id="updateById" parameterType="cn.springmybatis.entity.Emp">
update emp set salary=#{salary} where name=#{name}
</update>
</mapper>
5、获取SqlSession
package cn.springmybatis.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
public static SqlSession getSqlSession() {
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//加载SqlMapConfig.xml文件
String conf = "SqlMapConfig.xml";
InputStream confStream =
MyBatisUtil.class.getClassLoader().getResourceAsStream(conf);
//获取SqlSessionFactory
SqlSessionFactory factory = builder.build(confStream);
//获取SqlSession
SqlSession session = factory.openSession();
return session;
}
}
6、编写测试类
package cn.springmybatis.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import cn.springmybatis.entity.Emp;
public class TestEmp {
public static void main(String[] args) {
SqlSession session = MyBatisUtil.getSqlSession();
// List<Emp> listEmp =session.selectList("findAll");
// for(Emp emp: listEmp) {
// System.out.println("id:"+emp.getId()+", name:"+emp.getName()+", sex:"+emp.getSex()+", salary:"+emp.getSalary());
// }
// List<Emp> listEmp =session.selectList("findBySex",0);
// for(Emp emp: listEmp) {
// System.out.println("id:"+emp.getId()+", name:"+emp.getName()+", sex:"+emp.getSex()+", salary:"+emp.getSalary());
// }
// Emp emp = session.selectOne("findById", 3);
// System.out.println("id:"+emp.getId()+", name:"+emp.getName()+", sex:"+emp.getSex()+", salary:"+emp.getSalary());
// Emp emp = new Emp("校长",1,100000.0);
// int count =session.insert("insert", emp);
// System.out.println(count);
// int count = session.delete("deleteById", 1);
// System.out.println(count);
Emp emp = new Emp("小红",8000.0);
session.update("updateById", emp);
session.commit();//提交事务,增删改操作必须做事务提交
session.close();//释放session
}
}