MyBatis
1、MyBatis数据库配置文件 .properties
在resources 中创建
database.properties
文件(名称可改),然后直接粘贴如下内容
jdbc.driver=com.mysql.jdbc.Driver
# 如果使用mysql8.0,增加一个时区 的配置 以上 serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&userUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=wl86662165
- 如果使用mysql8.0,增加一个时区 的配置 以上 serverTimezone=Asia/Shanghai
2、MyBatis核心配置文件(spring整合前&后)
spring整合MyBatis 《前》.xml <configuration…>
就是连接数据库配置
在resources 中创建mybatis-config.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 核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<!-- 事务管理-->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&userUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="wl86662165"/>
</dataSource>
</environment>
</environments>
</configuration>
spring整合MyBatis 《后》.xml <configuration…>
就是连接数据库配置
在resources 中创建mybatis-config.xml
(名称可改) 文件,然后直接粘贴如下内容
- typeAliases:为取别名,可以选择性使用
- mappers:映射dao层的mapper.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 核心配置文件-->
<configuration>
<!-- 配置数据源-给spring去做-->
<typeAliases>
<package name="com.ctra.pojo"/>
</typeAliases>
<!-- 一个dao对应一个mapper-->
<mappers>
<mapper class="com.ctra.dao.BookMapper"/>
</mappers>
</configuration>
mapper.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="com.ctra.dao.UserMapper">
</mapper>
3、MyBatis-Spring 整合
spring-dao.xml (JDBC 版本) .xml <beans …>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
我们这里使用Spring 提供的JDBC:org.springframework.jdbc.datasource-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useSSL=false&userUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="wl86662165"/>
</bean>
<!-- sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/ctra/mapper/*.xml"/>
</bean>
<!-- sqlSessionTemplate:就是我们使用的sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 只能使用构造器注入sqlSessionFactory,因为它没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- 将实现类注入到bean中-->
<bean id="userMapper" class="com.ctra.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
Servlet 4.0 web.xml 配置 .xml <web-app…>
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
beans
1、基本结构 (官方文档:配置bean).xml <beans …>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
如下内容非拷贝内容,为原理性解释
- id : bean 的唯一标识符,相遇对象名
- class : bean 对象所对应的全限定名: 包名+类型
- name:也是别名,而且比 name 用法更加灵活(支持多别名)
<bean id="userT" class="com.ctra.pojo.UserT" name="user2 u2,u3;t2">
<property name="name" value="zhangsan"/>
</bean>