mybatis底层是对jdbc的封装
MyBatis的优点之一:不需要编写实现类,主需要写sql命令
mybatis.xml
IDEA搭建mybatis项目之异常:java.io.IOException: Could not find resource mapping/UserMapper.xml
mybatis操作mysql数据库Error querying database. Cause: java.sql.SQLException: Cannot convert value '0000-
属性名之间要写空格
mybatis通过setting控制全局开关
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="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/myblog"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Mapper/ArticleMapper.xml"/>
</mappers>
</configuration>
其中一个mapper
<?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的作用是编写需要执行的sql命令-->
<!--把xml理解成实现类-->
<!--把namespace理解成实现类的全路径(包名+类名)-->
<mapper namespace="Article">
<!--id就是方法名
parameterType=""定义参数类型
resultType=""返回值类型
如果返回值是list,在resultType中写list的泛型
因为mybatis中对jdbc封装,一行一行的读取数据
resultType中要写全路径,因为反射创建类要求全路径-->
<select id="selAll" resultType="Entity.Article">
SELECT * FROM t_article
</select>
</mapper>
一次提交多条sql语句:
conn.setAutoCommit(false);然后在finally里面conn.commit;
三种查询方式:
selectList(适用于查询结果都需要遍历的时候)
List<Article> list= session.selectList("Article.selAll");
for(Article article:list){
System.out.println(article.toString());
}
selectOne(适用于返回结果只是变量或一行数据的时候)
int count=session.selectOne("Article.selById");
根据id查询:
mapper中:
<select id="selById" resultType="Entity.Article" parameterType="int">
SELECT * FROM article WHERE id=#{0}
</select>
测试类:
Article article=session.selectOne("Article.selById",2);
System.out.println(article);
selectMap(适用于需要在查询结果中通过某列的值取到这行数据的需求,如通讯录。)
Map<Object,Object> map= session.selectMap("Article.selAll","title");
System.out.println(map);
mybatis中增删改不需要resultType,默认是int
mybatis中默认关闭自动提交
session.commit;提交事务
openSession(true)自动提交
mybatis接口绑定方案和多参数传递
1.作用:实现创建一个接口后把mapper.xml由mybaits生成接口的实现类,通过调用就接口对象就可以获取mapper.xml中编写的sql。
实现步骤:
1.创建一个接口,接口名和接口包名要和mapper中的namespace一致
2.在mybatis.xml中使用<package>进行扫描接口和mapper,package中传全路径比如“dao/ArticleDao”
注意:mapper.xml和相应同名接口要放一起。接口中方法名和mapper中方法名要相同。
service调用的时候通过session.getmapper使用(代理模式)
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession session = build.openSession();
ArticleMapper mapper = session.getMapper(ArticleMapper.class);
List<Article> articles = mapper.selAll();
for (Article l:articles) {
System.out.println(l);
}
动态SQL
根据不同条件执行不同SQL
mybatis中动态SQL就是在mapper中添加逻辑判断
接口中传入的参数要用@param注解:
接口中:
public interface ArticleMapper {
List<Article> selAll();
List<Article> selByIdAndTitle(@Param("id") String id, @Param("title") String title);
}
mapper中:
<select id="selByIdAndTitle" resultType="Entity.Article">
SELECT * FROM article
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="title!=null and title!=''">
and title=#{title}
</if>
</where>
</select>
where会把里面的内容中第一个and去掉,其他and保留,只输入一个条件的时候,去掉第一个and,sql语句为select * from article where id=? 输入两个条件的时候就变成了where id=? and title =?
service中:
System.out.println("输入id");
String id=reader.nextLine();
System.out.println("输入title");
String title=reader.nextLine();
List<Article> articles1 = mapper.selByIdAndTitle(id, title);
System.out.println(articles1);
注解方案
mybatis.xml中写
<package name="接口所在包的相对路径"/>
接口中写
@Select("SELECT * FROM article")
List<Article> selAll();