最近学习了一下MyBatis,在学习的过程中获得了一些启发,也遇到了一些困难,发此博客让初学者少走弯路,同时可以让自己的记忆更加深刻。
1.首先 File→New→Project
2.接下来直接点next→next→finish(名字随便起就行)
3根据目录来创建(MyBatis只需要导入三个包就行,如果你是5.0数据库则需要导入5.0数据库的包)
如果你是第一次创建xml文件 IDEA里并没有相应的模板 你只需要点击new→file,后缀名写.xml就行
4.City
package sc.domain;
public class City {
private int cid;
private String cname;
private int pid;
public City() {
}
public City(int cid, String cname, int pid) {
this.cid = cid;
this.cname = cname;
this.pid = pid;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
}
5.CityMapper
package sc.mapper;
import sc.domain.City;
import java.util.List;
public interface CityMapper {
public List<City> findAllUser();
}
6CityMapper.xml
<?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="sc.mapper.CityMapper">
<select id="findAllUser" resultType="sc.domain.City">
select * from city
</select>
</mapper>
7Test
package sc.Test;
import org.apache.ibatis.session.SqlSession;
import sc.domain.City;
import sc.mapper.CityMapper;
import sc.utils.MybatisUtil;
import java.util.List;
public class Test {
public static void main(String[] args) {
SqlSession session = MybatisUtil.getSession();
List<City> list= session.getMapper(CityMapper.class).findAllUser();
for (City city : list) {
System.out.println(city.getCname());
}
}
}
8.MybatisUtil
package sc.utils;
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;
public class MybatisUtil {
//加载mybatis配置文件
//创建sqlsessionfactory对象
//创建sqlsession连接对象
//创建关闭sqlsession连接对象
private static SqlSessionFactory sessionFactory;
static{
String resource="mybatis.xml";
try {
InputStream is=Resources.getResourceAsStream(resource);
sessionFactory=new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession getSession(){
return sessionFactory.openSession();
}
public static void closeSession(SqlSession session){
if(session!=null){
session.close();
}
}
}
9.mybatis.xml
<?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="sc">
<environment id="sc">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<!--配置源文件-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"></property>
<!--接口大部分都是3306,(我这里是3308),如果你数据库是5.0或者以下的这不需要加characterEncoding = utf8&&useSSL=false&serverTimezone=UTC-->
<property name="url" value="jdbc:mysql://localhost:3308/db_shopping?characterEncoding = utf8&&useSSL=false&serverTimezone=UTC"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="sc/mapper/CityMapper.xml"></mapper>
</mappers>
</configuration>
10数据库表
11运行结果
希望会对初学者有用。