导入相关架包
- Junit
- mybatis
- mysql数据库
- spring-jdbc(数据源,也可以用别的)
- mybatis-spring(新)
- aop织入
- spring相关的
Tips:Mybatis 是3.5+ 且spring 是5.0+ 用Mybatis-Spring 2.0+,其他就用Mybatis-Spring1.3.
Mybatis-Spring
- 连接数据库的数据源使用Spring-jdbc架包中的datesource,在spring的配置文件配置连接数据的信息,主要是配置sqlSessionFactory和sqlSessionTemplate。
<!--原来在Mybatis中,现在由spring管理-->
<bean id="datesource" class="org.springframework.jdbc,datesource.DriverManagerDatesource">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useSSL=true&useUnicode=true&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactoryBean,配置mybatis所有的东西,可以不需要mybatis配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dateSource" ref="datesource">
<!--绑定Mybatis配置文件,也可以在这里配置所有mybatis所有东西,就不需要mybatis配置文件了-->
<property name="configLocation" value="classpath:mybats-config.xml">
<property name="mapperLocation" value="classpath:mapper/*.xml"><!--mybatis中注册mapper的信息-->
</bean>
<!--sqlSessionTemplate模板,就相当于在mybatis中学的sqlSession-->
<!--将所有的mybatis配置放入到SQLSessionFactory,就相当于Mybatis中将配置文件弄成一个流放入到工厂中-->
<bean id ="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只有使用构造器注入sqlSessionFactory,因为他没有set方法-->
<constructor-arg index="0" ,ref="sqlSessionFactory">
</bean>
官方的整合方式
- 自己写的不是很详细,但是官网有很详细的步骤,可以去官网看看,超级详细 官方的整合
网友评论