我的第一个mybatis程序
程序员文章站
2024-03-08 16:20:30
...
1、首先建立maven项目
新建项目后可能会出现jre版本不一致的问题,需要修改他的属性,如下图所示:
2、修改pom.xml,添加依赖库
需要添加mybatis和jdbc的jar包,pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.luo.my</groupId>
<artifactId>mybatis1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mybatis1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</project>
3、建立数据库
使用的是mysql。建立了3张表,分别是学生表、课程表和选课表,但这只是个入门程序,所以实际上只用到了学生表。
drop table if exists SC cascade;
drop table if exists Student cascade;
drop table if exists Course cascade;
create table Student(
Sno char(9) primary key,
Sname char(20) unique,
Ssex char(1),
Sage smallint,
Sdept char(20)
);
create table Course(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit SMALLINT
);
create table SC(
Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
foreign key (Sno) REFERENCES Student(Sno),
foreign key (Cno) references Course(Cno)
);
insert into Student values('201215121','李勇','男',20,'CS'),('201215122','刘晨','女',19,'CS'),('201215123','王敏','女',18,'MA'),('201215125','张立','男',19,'IS');
insert into Course values('1','数据库','5',4),('2','数学',null,2),('3','信息系统','1',4),('4','操作系统','6',3),('5','数据结构','7',4),('6','数据处理',null,2),('7','PASCAL语言','6',4);
insert into SC values('201215121','1',92),('201215121','2',85),('201215121','3',88),('201215122','2',90),('201215122','3',80);
4、项目目录结构
4.1、在访问数据库后,学生数据将存入到Student的对象当中,因此需要编写一个Student的pojo类Student.java:
package com.luo.my.pojo;
public class Student {
private String Sno;
private String Sname;
private String Ssex;
private int Sage;
private String Sdept;
public void print() {
System.out.println("Sno:"+Sno);
System.out.println("Sname:"+Sname);
System.out.println("Ssex:"+Ssex);
System.out.println("Sage:"+Sage);
System.out.println("Sdept:"+Sdept);
}
public String getSno() {
return Sno;
}
public void setSno(String sno) {
Sno = sno;
}
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public int getSage() {
return Sage;
}
public void setSage(int sage) {
Sage = sage;
}
public String getSdept() {
return Sdept;
}
public void setSdept(String sdept) {
Sdept = sdept;
}
}
4.2、执行sql语句,需要调用对应的方法,因此建立一个映射类StudentMapper.java:
package com.luo.my.dao;
import com.luo.my.pojo.Student;
public interface StudentMapper {
public Student selectStudent(String Sno);
}
4.3、建立StudentMapper.xml文件,当调用上述StudentMapper的方法时,会自动执行这个xml文件中对用xml中的sql语句
<?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">
<mapper namespace="com.luo.my.dao.StudentMapper">
<select id="selectStudent" resultType="com.luo.my.pojo.Student">
select * from Student where Sno = #{id}
</select>
</mapper>
select代表着sql的select语句,id一定要和StudentMapper类的方法同名,这样才会有关联。resultType表示sql执行后返回的类型。
4.4在mybatis总的配置文件中配置数据源和指定mapper文件的位置。
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/luo/my/dao/StudentMapper.xml"/>
</mappers>
</configuration>
4.5建立App.java测试是否能读出数据
package com.luo.my.mybatis1;
import java.io.IOException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.luo.my.dao.StudentMapper;
import com.luo.my.pojo.Student;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
// System.out.println( "Hello World!" );
SqlSessionFactory sqlSessionFactory=null;
try {
sqlSessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SqlSession sqlSession=sqlSessionFactory.openSession();
StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
Student student=studentMapper.selectStudent("201215122");
if(student!=null) {
student.print();
}else {
System.out.println("nothing");
}
}
}
4.6输出结果
表示mybatis项目可以成功执行了。
5总结
总的来说,mybatis的关键就是建立一个Mapper.xml和对应的接口类。通过xml文件中的描述产生一个和接口类型一致的对象,使用这个对象来执行对应的sql文件。当sql执行后需要保存他的数据,那么需要一个pojo对象来存储sql执行后的值。
所以说最关键的还是Mapper.xml文件的编写,本例子中的mapper文件相对简单,更复杂的需要设计到复杂sql语句的编写和级联问题。
下一篇: 详解Java设计模式编程中的中介者模式