mybatis和对应的数据库驱动
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.9version>
dependency>
<dependency>
<groupId>org.postgresqlgroupId>
<artifactId>postgresqlartifactId>
<version>42.3.3version>
dependency>
dependencies>
testdemo表内有username和age字段,实体类如下
public class TestDemo {
//在没有使用另名的情况下,这里需要和数据库字段名称一致
private String username;
private int age;
@Override
public String toString() {
return "TestDemo{" +
"username='" + username + '\'' +
", age='" + age + '\'' +
'}';
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
每个xml文件都有对应的namespace
即mybatis是通过名称空间来查找对应的java类或者接口
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cq.mybatis.maaper">
<select id="select" resultType="com.cq.mybatis.module.TestDemo">
select * from test_demo
select>
mapper>
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="sql driver"/>
<property name="url" value="you url"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
dataSource>
environment>
environments>
<mappers>
<mapper resource="usermapper.xml"/>
mappers>
configuration>
运行时找不到包的处理方法
默认情况下,只会去查找resources文件下的配置文件,且不会编译进去
在maven配置文件中增加如下配置
<build>
<resources>
<resource>
<directory>/src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>/src/main/resourcesdirectory>
resource>
resources>
build>
#5调用方式
使用TestNg新建测试类的方式,也可以直接写一个main方法
public class test {
SqlSession sqlSession;
@BeforeTest
public void setup() throws IOException {
//读取mybatis配置文件
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatisConfig.xml"));
sqlSession = factory.openSession();
}
@Test
public void testSelect(){
//读取对应的sql
// selectList的参数是mapper.xml中 namespace.id 的方式
List<TestDemo> demos = sqlSession.selectList("com.cq.mybatis.maaper.TestDemo.select");
for (TestDemo demo:
demos) {
System.out.println(demo);
}
}
@AfterTest
public void teardown(){
sqlSession.close();
}
}
实体类不用变,需要创建对应的配置文件映射接口,再创建对应的配置文件
public interface UserMapper {
//注意是,这里的方法名和xml文件中的id一致,否则会找不到调用
List<TestDemo> getDemos();
}
注意
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cq.mybatis.maaper.UserMapper">
<select id="getDemos" resultType="com.cq.mybatis.module.TestDemo">
select * from test_demo
select>
mapper>
在mybatis配置文件中,修改mapper的查找方式如下
即接口文件和配置文件所在的包下
<mappers>
<package name="com.cq.mybatis.maaper"/>
mappers>
在方法一的基础上,增加一个读取mapper的语句
public class test {
SqlSession sqlSession;
UserMapper mapper;
@BeforeTest
public void setup() throws IOException {
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatisConfig.xml"));
sqlSession = factory.openSession();
// 读取接口给变量,然后由mybatis去查找对应sql语句
mapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void testSelect(){
// List demos = sqlSession.selectList("com.cq.mybatis.maaper.TestDemo.select");
List<TestDemo> demos = mapper.getDemos();
for (TestDemo demo:
demos) {
System.out.println(demo);
}
}
@AfterTest
public void teardown(){
sqlSession.close();
}
}