Getting Started with java – MyBatis
The real power of MyBatis lies in its mapping statement, which is also its magic. Because of how powerful it is, the mapper's XML file is relatively simple. If you compare this with the JDBC code that has the same functionality, you'll immediately see that you've saved almost 95% of the code. MyBatis is built for SQL and does it better than normal.
The SQL mapping file has a few top-level elements (in the order they should be defined):
cache
– Cache configuration for a given namespace.cache-ref
– References to other namespace cache configurations.resultMap
– is the most complex and powerful element, used to describe how to load objects from the database result set.sql
– A reusable block of statements that can be referenced by other statements.insert
– Map insert statements.update
– Mapping update statements.delete
– Map delete statements.select
– Map query statement.
If you use Maven
to build the project, you need to put the following dependency code in pom.xml
the file.
Simple example (need to import shelf packages: database driver shelf package and mybatis shelf package):
Entity classes are still used UserInfo
.
mapper
kind:
package cn.zbw.mapper;
import cn.zbw.entity.UserInfo;
import java.util.List;
public interface UserInfoMapper {
public List<UserInfo> findUserAll();
}
mapper.xml
File configuration:
<?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="cn.zbw.mapper.UserInfoMapper">
<select id="findUserAll" resultType="cn.zbw.entity.UserInfo">
select * from user
</select>
</mapper>
Tools:
package cn.zbw.utils;
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 java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis.xml";
try {
InputStream is = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sqlSessionFactory.openSession();
}
public static void closeSession(SqlSession sqlSession){
if (sqlSession!=null){
sqlSession.close();
}
}
}
mybatis.xml
File configuration:
<?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="zbw">
<environment id="zbw">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/zh_demo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</dataSource>
</environment>
</environments>
<!--配置接口所对应的xml文件-->
<mappers>
<mapper resource="cn/zbw/mapper/UserInfoMapper.xml"></mapper>
</mappers>
</configuration>
Test class:
package cn.zbw.test;
import cn.zbw.entity.UserInfo;
import cn.zbw.mapper.UserInfoMapper;
import cn.zbw.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
test01();
}
private static void test01() {
SqlSession session= MybatisUtil.getSession();
List<UserInfo> lists=session.getMapper(UserInfoMapper.class).findUserAll();
for (UserInfo u:lists){
System.out.println(u.getUsername());
}
}
}
turn out: