知識(shí)科普知識(shí)科普
spring常用配置文件中頭文件信息
1、applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定要掃描的包,這個(gè)包下的注解就會(huì)生效-->
<context:component-scan base-package="com.xiong"/>
<!--開啟注解支持-->
<context:annotation-config/>
</beans>
2、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>
<!--引入外部配置文件-->
<properties resource="db.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--給實(shí)體類起別名-->
<typeAliases>
<package name="com.xiong.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--每一個(gè)mapper.xml都需要在Mybatis的核心配置文件中注冊(cè)!-->
<mappers>
<mapper class="com.xiong.dao.TeacherMapper"/>
<mapper class="com.xiong.dao.StudentMapper"/>
</mappers>
</configuration>
3、Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiong.dao.TeacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname ,t.id tid
from mybatis.student s,mybatis.teacher t
where s.tid = t.id and t.id = #{tid};
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<!--復(fù)雜的屬性單獨(dú)處理 對(duì)象:association 集合:collection
javaType=""
集合中的泛型類型,我們用ofType獲取
-->
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
4、db.properties
driver = com.mysql.jdbc.Driver
#MySQL 8.0.20版本 url中必須添加serverTimezone屬性:&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL
url=jdbc:mysql://localhost:3306/smbms?useSSL=true&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
5.application.proprtties
#更改項(xiàng)目端口號(hào)
server.port=8080
#關(guān)閉thymeleaf緩存
spring.thymeleaf.cache=false
#國(guó)際化配置文件的真實(shí)的路徑
spring.messages.basename=i18n.login
#數(shù)據(jù)庫
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/company?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis
mybatis.type-aliases-package=com.xiong.pojo
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml