Maven环境下的Mybatis-HelloWorld
(单独学mybatis参考:http://www.mybatis.org/mybatis-3/zh/index.html ,学spring与mybatis:http://www.mybatis.org/spring/zh/index.html)
1.首先,新建Maven工程,在pom.xml文件中添加依赖(mybatis,mysql,junit...暂时只用这么多)
<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>mybatis3</groupId> <artifactId>com</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- mybatis依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <!-- mysql连接依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- junit单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>
2.准备mybatis配置文件,mybatis-config.xml,在mybatis中文网上拷贝
<?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://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <mappers> <mapper resource="BlogMapper.xml" /> </mappers> </configuration>
3.写bean与Mapper
package com.entity;
public class Blog {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Blog() {
}
public Blog(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Blog [id=" + id + ", name=" + name + "]";
}
}
package com.mapper;
import com.entity.Blog;
public interface BlogMapper {
public Blog selectBlog(Integer id);
}
<?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.mapper.BlogMapper">
<select id="selectBlog" resultType="com.entity.Blog">
select * from blog where id = #{id}
</select>
</mapper>
4.写测试类进行测试
package com.test;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.Test;
import com.entity.Blog;
import com.mapper.BlogMapper;
public class BlogTest {
private SqlSessionFactory getSqlSessionFactory() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
@Test
public void test() throws Exception {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
BlogMapper mapper = openSession.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(1);
System.out.println(blog);
} finally {
openSession.close();
}
}
}
需提前准备Mysql数据库,Bolg表(id,name)两个字段
遇到的问题:
1.找不到mybatis-config配置文件
解决办法,将配置文件放在/com/src/main/resources目录下,访问时,直接给名称即可
2.BlogMapper.xml文件放在com.mapper下得不到加载,找不到mapper.xml文件问题
解决办法,将配置文件放在/com/src/main/resources目录下,访问时,直接给名称即可
3.mapper.xml文件的命名空间名称与mapper.java文件包名不匹配(Type interface com.mapper.XMapper is not known to the MapperRegistry)问题
解决方法,将mapper.xml的namespace属性值写成与之对应的mapper.java文件的所在包名即可
原文:https://www.cnblogs.com/superhonors/p/11291585.html