对数据表Customer进行增删查改:
导入jar包:
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>
<!-- 配置数据库连接环境:driver、url、username、password -->
<environments default="mysql">
<!-- 开始配置mysql -->
<environment id="mysql">
<!--配置事务 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 关联局部配置文件 -->
<mappers>
<mapper resource="com/no7/mapper/CustomerMapper.xml"/>
<!-- 以接口方式 -->
<mapper class="com.no7.mapper.CustomerMapper2"></mapper>
</mappers>
</configuration>
实体类创建:
public class Customer {
private int id;
private String username;
private String jobs;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getJobs() {
return jobs;
}
public void setJobs(String jobs) {
this.jobs = jobs;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", username='" + username + '\'' +
", jobs='" + jobs + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
接口类:
public interface CustomerMapper2 {
Customer findCustomerByVo(QueryVo qv);
Customer findCustomerById(Integer id);
}
mybatis映射文件:
<?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:命名空间,其值为某一个dao层类的具体路径
-->
<mapper namespace="com.no7.mapper.CustomerMapper">
<!-- sql语句保存在Mybatis的局部配置文件中 -->
<!--
select标签存放查询语句(List<User>)
id:在整个配置文件中id值必须唯一,其值与dao层类中的方法名保持一致
resultType:指定当前sql查询语句返回的数据类型。类型不是为sql语句的最终类型,而是某一条数据的类型
parameterType:参数类型
-->
<select id="findCustomerById" resultType="com.no7.po.Customer" parameterType="Integer">
SELECT * FROM customer where id = #{id}
</select>
<select id="findCustomerByName" resultType="com.no7.po.Customer" parameterType="String">
<!--SELECT * FROM customer where username = #{username} -->
SELECT * FROM customer where username LIKE CONCAT('%',#{username},'%') <!-- CONCAT字符串拼接 -->
</select>
<select id="findCustomerByVo1" resultType="com.no7.po.Customer" parameterType="com.no7.vo.QueryVo">
SELECT * FROM customer where username = #{com.no7.po.Customer.username}
</select>
<insert id="insertCustomer" parameterType="com.no7.po.Customer" >
<!--INSERT INTO customer SET username=#{username},password=#{password} -->
INSERT INTO customer (username,jobs,phone) values (#{username},#{jobs},#{phone})
</insert>
<update id="updateCustomer" parameterType="com.no7.po.Customer">
UPDATE customer SET username=#{username},jobs=#{jobs},phone=#{phone} WHERE id = #{id}
</update>
<delete id="deleteCustomer" parameterType="Integer">
delete from customer where id = #{id}
</delete>
</mapper>
编写MybatisUtils.java文件
mport org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MybatisUtils {
public static SqlSessionFactory sqlSessionFactory=null;
private MybatisUtils(){
}
public static SqlSessionFactory getSqlSessionFactory(){
synchronized(MybatisUtils.class) {
if (sqlSessionFactory != null)
return sqlSessionFactory;
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("com/no7/resource/mybatis.xml");
sqlSessionFactory = sfb.build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession(){
if (sqlSessionFactory==null)
getSqlSessionFactory();
return sqlSessionFactory.openSession();
}
}
测试类
public class MybatisTest1 {
public static void main(String[] args) throws IOException {
// find(1);
//insert();
//update();
//delete(4);
findVo();
find2(1);
findVo2();
}
public static void find(Integer id) throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
//Customer c = session.selectOne("findCustomerById",id);
//Customer c = session.selectOne("findCustomerByName","joy");
List<Customer> cs = session.selectList("findCustomerByName","o");
for (Customer c:cs) {
System.out.println(c);
}
session.close();
}
public static void find2(Integer id) throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
CustomerMapper2 customerMapper2 = session.getMapper(CustomerMapper2.class);
Customer c = customerMapper2.findCustomerById(id);
System.out.println(c);
session.close();
}
public static void findVo() throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
//Customer c = session.selectOne("findCustomerById",id);
//Customer c = session.selectOne("findCustomerByName","joy");
List<Customer> cs = session.selectList("findCustomerByVo1","joy");
for (Customer c:cs) {
System.out.println(c);
}
session.close();
}
public static void findVo2() throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
CustomerMapper2 customerMapper2 = session.getMapper(CustomerMapper2.class);
Customer c = new Customer();
c.setId(4);
c.setUsername("joy");
c.setJobs("teacher");
QueryVo qv = new QueryVo();
c.setPhone("123456");
qv.setCustomer(c);
c = customerMapper2.findCustomerByVo(qv);
if (c !=null) {
System.out.println(c);
}else{
System.out.println("NOT FOUND");
}
session.close();
}
public static void insert() throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
Customer c = new Customer();
c.setUsername("abc");
c.setJobs("teacher");
c.setPhone("123456");
int t = session.insert("insertCustomer",c);
session.commit();
if (t>0){
System.out.println("insert" + t);
}
session.close();
}
public static void update() throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
Customer c = new Customer();
c.setId(4);
c.setUsername("rose");
c.setJobs("teacher");
c.setPhone("123456");
int t = session.update("updateCustomer",c);
session.commit();
if (t>0){
System.out.println("update" + t);
}
session.close();
}
public static void delete(Integer id) throws IOException {
SqlSession session = MybatisUtils.openSqlSession();
int t = session.delete("deleteCustomer",id);
session.commit();
if (t>0){
System.out.println("delete" + t);
}
session.close();
}
}