Mybatis配置方式:
方式一:不用接口:表对应类+.xml
方式二:用mapper:表对应类+类Mapper接口+.xml
方式三:使用注解:表对应类+类Mapper接口
当然还有SQL语句方式,在另一篇文章
https://blog.csdn.net/beauman/article/details/90751393
~~
~~
用最简单的只有 id 和name的表演示
对应World类
public class World {
int id;
String name;
@Override
public String toString() {
return "World{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
因为我的类创建再pojo文件下,我为了好看在resources下创建了一个pojo而真正决定起作用的和路径无关,只要在mybatis下
world.xml直接放在resources也可以
1.world.xml代码:
只演示一个select
2.mybatis.xml配置
3.Test
public class WorldTest {
@Test
public void worldt() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
CategoryMapper mapper = session.getMapper(CategoryMapper.class);
List worlds = session.selectList("listWorld");
for (World w:worlds
) {
System.out.println(w);
}
session.commit();
session.close();
}
}
1.worldMapper
public interface WorldMapper {
void addWorld();
List listWorld();
}
2.WorldMapper.xml
注意点:
1.还是那句话,名字和位置是随便起,随便放,但是 你得在 mybatis.xml中对应准确:
2.此时的namespace的值必须为WorldMapper接口
代码:
3.Mybatis.xml
只需要改
全部代码:
4.Test
public class WorldTest {
@org.junit.Test
public void test1() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//读取配置文件的配置信息,利用SqlSessionFactoryBuilder创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//利用sqlSessionFactory打开与数据库的会话
SqlSession sqlSession = sqlSessionFactory.openSession();
WorldMapper worldMapper = sqlSession.getMapper(WorldMapper.class);
List lists = worldMapper.listWorld();
for (World w : lists
) {
System.out.println(w);
}
sqlSession.commit();
sqlSession.close();
}
1.WorldMapper2
public interface WorldMapper2 {
@Select("select * from world")
List listWorld2();
}
2.mybatis.xml配置
只修改下面就行了
3.Test
public class WorldMapperTest {
@Test
public void t() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//读取配置文件的配置信息,利用SqlSessionFactoryBuilder创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//利用sqlSessionFactory打开与数据库的会话
SqlSession sqlSession = sqlSessionFactory.openSession();
WorldMapper2 mapper2 = sqlSession.getMapper(WorldMapper2.class);
listWorld(mapper2);
sqlSession.close();
}
private static void listWorld(WorldMapper2 mapper2){
List ws = mapper2.listWorld2();
for (World w:ws
) {
System.out.println(w);
}
}
}
方式一的启动方式是session.
方式三的启动方式是
先getMapper
WorldMapper mapper = sqlSession.getMapper(WorldMapper.class);
再调用mapper.
在WorldMapper中自己设置的方法。
方式二因为既有mapper又有xml所以
session和mapper都可以启动,
上面代码给的是mapper启动,这是session启动,效果一样,看你想用哪个
SqlSession sqlSession = sqlSessionFactory.openSession();
List worlds = sqlSession.selectList("listWorld");
for (World w:worlds
) {
System.out.println(w);
}
方式一、二是xml配置方式
在mybatis.xm需要用
而方式三不用xml只保留了mapper
方式二中xml的
必须指定到mapper