JDBC连接数据库的步骤都很固定,只有SQL语句和封装结果不一样。所以只需要把相同的部分封装成框架就会很便捷。
MVC
框架:简化了
Servlet
的开发步骤(负责和前端进行交互,拿到前端数据)
Struts
Struts2
SpringMVC
持久层框架:完成数据库操作的框架(负责将前端数据存储到数据库,替代JDBC)
apache DBUtils
Hibernate(全自动框架)
Spring JPA
MyBatis(半自动框架)
EJB3.0
胶⽔框架:
Spring
MyBatis
是⼀个
半⾃动
的
ORM
框架
ORM
(
Object Relational Mapping
)对象关系映射,将
Java
中的⼀个对象与数据表中⼀⾏记录⼀⼀对应。
ORM
框架提供了实体类与数据表的映射关系,通过映射⽂件的配置,实现对象的持久化。
mybatis事务管理器:JDBC和Managed
IDEA连接数据库才能使用mybatis.user 否则直接使用user表
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from mybatis.user where id = #{id}
</select>
学习点:
- 将数据库配置文件外部引入
- 实体类别名
- 保证UserMapper 接口 和 UserMapper .xml 改为一致!并且放在同一个包下
【会发生的错】
- 实体类没有起别名
- 接口对应的mapper.xml文件中namespace命名错误
- 如果使用Lombok插件偷懒 1.在实体类上面要写@注解 2.导入jar包
- maven记得刷新
- maven静态资源过滤问题
- 在build标签中添加
-
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources>
select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid = t.id and t.id=#{tid}