(1)导入jar包
(2)需要四个文件
a、database.properties->资源文件,写入连接数据库需要的数据
b、config.xml->配置数据连接池,连接数据库使用
c、实体类名+Mapper.java->一个接口文件
d、实体类名+Mapper.xml->该文件实现了接口文件的方法
注意:实体类就是定义的Bean;c和d两文件最好放在同一个文件夹下;实体类名+Mapper.xml的作用效果:代替了DAO层,实现了接口中具体的方法
(3)测试
(4)以上使用的代码的合集(PS:比截图代码要全面)
League.java实体类
package cwu.chang.MyBaties;
public class League {
//使用Mybatis需要属性名与字段名完全匹配
public int lid;
public int lyear;
public String season;
public String title;
/*String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}*/
public League(){}
public int getLid() {
return lid;
}
public void setLid(int lid) {
this.lid = lid;
}
public int getLyear() {
return lyear;
}
public void setLyear(int lyear) {
this.lyear = lyear;
}
public String getSeason() {
return season;
}
public void setSeason(String season) {
this.season = season;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public League(int lid, int lyear, String season, String title) {
super();
this.lid = lid;
this.lyear = lyear;
this.season = season;
this.title = title;
}
@Override
public String toString() {
return "League [lid=" + lid + ", lyear=" + lyear + ", season=" + season
+ ", title=" + title + "]";
}
}
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>
<!-- 引用资源文件 resource=包名.类名-->
<properties resource="cwu/chang/MyBaties/database.properties" />
<!-- 配置实体类 alias=类的别名,自定义 type=包名.类名(实体类的位置)-->
<typeAliases>
<typeAlias alias="league" type="cwu.chang.MyBaties.League" />
</typeAliases>
<environments default="simple">
<environment id="simple">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- ${driver}为获取配置文件中name为driver的key值 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!-- 配置实力类名+Mapper.xml文件,因为该文件需要使用此资源文件来实现方法 -->
<mappers>
<mapper resource="cwu/chang/MyBaties/LeagueMapper.xml" />
</mappers>
</configuration>
LeagueMapper.java(定义了增删查改)
package cwu.chang.MyBaties;
import java.util.List;
public interface LeagueMapper {
public List<League> getAllLeague();
public League getLeagueById(int lid);
public void createLeague(League league);
public List<League> getLeagueByYear(int year);
public void updateLeague(League league);
public void deleteLeague(int lid);
}
LeagueMapper.xml(实现了接口的所有方法)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3.dtd">
<!-- namespace=包名。类名 -->
<mapper namespace="cwu.chang.MyBaties.LeagueMapper">
<!-- id=与.java文件中的方法名对应 parameterType=方法参数类型(这个方法没有,所以不要)
resultType=返回的结果集的数据类型(默认为list所以不需要写list<League>) -->
<!-- <select id="getAllLeague" parameterType="int" resultType="league"> -->
<select id="getAllLeague" resultType="league">
<!-- #{lid}使用EL表达式获取参数的值 与参数名字一样 -->
<!-- select * from league where lid = #{lid} -->
select * from league
</select>
<select id="getLeagueById" parameterType="int" resultType="league">
select * from league where lid = #{lid}
</select>
<insert id="createLeague" parameterType="league"
useGeneratedKeys="true" keyProperty="lid">
insert into league (lyear, season, title)
values (#{lyear}, #{season}, #{title})
</insert>
<update id="updateLeague" parameterType="league">
update league set
lyear = #{lyear},
season = #{season},
title = #{title} where lid = #{lid}
</update>
<delete id="deleteLeague" parameterType="int">
delete from league where lid = #{lid}
</delete>
<select id="getLeagueByYear"
parameterType="java.lang.Integer" resultType="league">
select * from league where lyear = #{lyear}
</select>
</mapper>
MyBatisTest.java
package cwu.chang.MyBaties;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisTest {
private static SqlSessionFactory sqlSessionFactory;
static {
try{
String resource = "cwu/chang/MyBaties/config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch(IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(boolean autoCommit) {
return sqlSessionFactory.openSession(autoCommit);
}
public static void main(String[] args) {
SqlSession sqlSession = MyBatisTest.getSession(true);
LeagueMapper leagueMapper = sqlSession.getMapper(LeagueMapper.class);
try{
System.out.println("---------- 查询所有的League对象 -----------");
List<League> leagues = leagueMapper.getAllLeague();
for (League league : leagues) {
System.out.println(league.getLid()+" "+league.getLyear()+" "+league.getSeason()+" "+league.getTitle());
}
System.out.println("---------- 查询lid=1的League对象 -----------");
League league = leagueMapper.getLeagueById(1);
System.out.println(league);
System.out.println("------------ 插入新建League对象-------------");
league = new League(-1, 2016, "Winter", "2016 Winter League");
leagueMapper.createLeague(league);
System.out.println(league);
System.out.println("------------ 查询现有League对象-------------");
List<League> list = leagueMapper.getLeagueByYear(3);
for (League l : list) {
System.out.println(l);
}
System.out.println("------------ 修改现有League对象-------------");
league.setSeason("Summer");
league.setTitle("2016 Summer League");
leagueMapper.updateLeague(league);
System.out.println(league);
System.out.println("------------ 删除现有League对象-------------");
leagueMapper.deleteLeague(league.getLid());
System.out.println("已删除league,lid=" + league.getLid());
} catch(Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
}
}