准备环境:安装jdk 、maven、mysql、Intellij IDEA
官方文档:http://www.mybatis.org/mybatis-3/zh/index.html
示例步骤
1.新建maven项目
2.在pom.xml中配置mybatis、mysql依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
3.启动本地mysql服务,创建mysql数据库student,数据库中创建student表
4.创建Student类,类中属性与student表中字段一 一对应
public class Student{
int id;
String name;
String sex;
public Student(){
}
public Student(int id,String name,String sex){
this.id=id;
this.name=name;
this.sex=sex;
}
public int getId(){
returnid;
}
public void setId(intid){
this.id=id;
}
public String getName(){
returnname;
}
public void setName(String name){
this.name=name;
}
public String getSex(){
returnsex;
}
public void setSex(String sex){
this.sex=sex;
}
@Override
public String toString(){
return"Student{"+
"id="+id+
",name='"+name+'\''+
",sex='"+sex+'\''+
'}';
}
}
5.创建studentMapper.xml,在此文件中管理sql语句
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC"-//mybatis.org//DTDMapper3.0//EN"
"[http://mybatis.org/dtd/mybatis-3-mapper.dtd](http://mybatis.org/dtd/mybatis-3-mapper.dtd)">
<mapper namespace="orm.mapper.studentMapper">
<!--resultType:返回值类型 parameterType:参数类型-->
<selectid="selectById"
resultType="orm.entity.Student" parameterType="int">
select*from student where id=#{id}
</select>
</mapper>
6.创建config.xml配置文件,配置数据库连接信息、mapper信息
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEconfiguration
PUBLIC"-//mybatis.org//DTDConfig3.0//EN"
"[http://mybatis.org/dtd/mybatis-3-config.dtd](http://mybatis.org/dtd/mybatis-3-config.dtd)">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!--此处配置数据库连接信息
type="POOLED":为用链接池方式访问数据库-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student"/>
<property name="username" value="root"/>
<property name="password" value="xiaofu"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="orm/mapper/studentMapper.xml"/>
</mappers>
</configuration>
7.测试一下
public class FirstBasicDemoTest{
public static void main(String[] args){
//获取sqlsession对象
String resource="firstBasicDemoConfigure.xml";
Reader reader=null;
try{
reader=Resources.getResourceAsReader(resource);
}catch(IOExceptione){
e.printStackTrace();
}
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
SqlSessionsession=sqlSessionFactory.openSession();
//进行查询操作
Stringstatment="orm.mapper.studentMapper.selectById";
Studentstudent=session.selectOne(statment,1);
System.out.println(student);
}
}
运行结果:
运行程序时可能会报错:
mybatis读取配置文件报错:Could not find resource firstBasicDemoConfigure.xml
原因:在classes目录下没有找到配置文件,所以配置文件没有在编译范围
解决:在pom.xml中添加下方配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>