mybatis入门——mybatis使用实例

时间:2021-12-15 05:13:33

mybatis区别于其他ORM工具的地方是,他不需要dao成实现类,只提供接口,数据库操作的方法写到配置文件中。
贴个例子来理解下:
测试项目结构:
mybatis入门——mybatis使用实例

mybatis-config.xml:

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
<environments default="yitu">
<environment id="yitu">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/yitu"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--
配置映射文件
-->

<mappers>
<mapper resource="com/etoak/dao/CityDaoIf-mapper.xml"/>
</mappers>
</configuration>

CityDaoIf-mapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.etoak.dao.CityDaoIf" >
<insert id="addCity" parameterType="com.etoak.bean.City">
insert into city values(null,#{pid},#{name});
</insert>
<delete id="delCityById" parameterType="java.lang.Integer">
delete from city where id=#{id};
</delete>
<update id="updateCity" parameterType="com.etoak.bean.City">
update city set pid=#{pid},name=#{name} where id=#{id};
</update>
<!--
resultMap: 返回数据类型
需要在下文中解释
-->

<select id="selectCityById" parameterType="java.lang.Integer" resultMap="city">
select * from city where id=#{id};
</select>
<!--
这里返回的是list,但list里面存放的还是city对象,所以还是city
-->

<select id="selectAllCitys" resultMap="city">
select * from city;
</select>
<select id="selectCityCount" resultType="java.lang.Integer">
select count(*) from city;
</select>
<select id="selectCityByPage" parameterType="java.util.Map" resultMap="city">
select * from city limit #{start},#{max};
</select>


<!--
返回类型解释
-->

<resultMap type="com.etoak.bean.City" id="city">
<result property="id" column="id"/>
<result property="pid" column="pid"/>
<result property="name" column="name"/>
</resultMap>
</mapper>

CityDaoIf.java

package com.etoak.dao;

import java.util.List;
import java.util.Map;

import com.etoak.bean.City;

public interface CityDaoIf {
/**
* 使用mybatis时需注意:
* 1 添加、删除、修改 返回值为 int类型
* 影响的行数 ==1 判断是否成功
* 2 如果方法涉及到传入的参数量多余一条;需要封装成map
*/

public int addCity(City city);
public int delCityById(Integer id);
public int updateCity(City city);
public City selectCityById(Integer id);
public List<City> selectAllCitys();
public int selectCityCount();
// map: start end
public List<City> selectCityByPage(Map map);
}

MybatisFactory.java

package com.etoak.factory;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisFactory {
private static SqlSessionFactory factory;
static{
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory(){
return factory;
}
}

实体类City.java

package com.etoak.bean;


public class City implements java.io.Serializable {



private Integer id;
private Integer pid;
private String name;


public City() {
}

public City(Integer pid, String name) {
this.pid = pid;
this.name = name;
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getPid() {
return this.pid;
}

public void setPid(Integer pid) {
this.pid = pid;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}

测试类:

package com.etoak.test;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.etoak.bean.City;
import com.etoak.dao.CityDaoIf;
import com.etoak.factory.MybatisFactory;

public class Test {
public static void main(String[] args){
SqlSessionFactory factory = MybatisFactory.getSqlSessionFactory();
SqlSession session = factory.openSession();
CityDaoIf dao = session.getMapper(CityDaoIf.class);
int add = dao.addCity(new City(55,"e"));
int del = dao.delCityById(4);
City c = new City();
c.setId(3);
c.setPid(1);
c.setName("aaaa");
int update = dao.updateCity(c);
City city = dao.selectCityById(3);
Map map = new HashMap();
map.put("start", 1);
map.put("max", 5);
//List list = dao.selectCityByPage(new Map());

//System.out.println("add="+add+"del="+del+"update="+update);
System.out.println(dao.selectCityByPage(map));
session.commit();
session.close();

}
}