本人小白,xml配置文件的头着实让我头疼,索性总结一下方便使用
Mybatis核心配置文件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>
Mybatis映射文件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="">
</mapper>
springmvc-servlet.xml配置文件
(可以直接去spring手册上找)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="org.example.web"/>
<!-- ... -->
</beans>
web.xml配置文件
<?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_3_1.xsd"
version="3.1" metadata-complete="true">
<!-- -->
</web-app>
spring的核心配置文件applicationContext.xml
(直接去spring手册上找的)
<?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
http://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>
Mybatis动态创建表
<update id="addTable" parameterType="Map">
CREATE TABLE ${tableName}(
id int(10) unsigned NOT NULL AUTO_INCREMENT,
<foreach collection="tableColumns" item="value">
${value} varchar(20) DEFAULT NULL,
</foreach>
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8;
</update>
List<String> optionsDataForm;和List<String> describeDataForm;的读和写
<?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="physical.examination.record.dao.DataFormDao">
<select id="getDataForm" resultMap="dataform">
select *
from tryAndTry
</select>
<resultMap type="entity.DataFormEntity" id="dataform">
<result column="Id" property="Id" jdbcType="INTEGER" />
<collection property="describeDataForm" ofType="java.lang.String">
<result column="describeDataForm" />
</collection>
<collection property="optionsDataForm" ofType="java.lang.String">
<result column="optionsDataForm" />
</collection>
</resultMap>
<insert id="addDataFormStandard" parameterType="entity.DataFormEntity">
insert into
tryAndTry(describeDataForm,optionsDataForm)
values(
<foreach collection="describeDataForm" item="describeDataForm">#{describeDataForm}
</foreach>
,
<foreach collection="optionsDataForm" item="optionsDataForm">#{optionsDataForm}
</foreach>
)
</insert>
</mapper>
(新手入门暂时遇到这些,后续再补充)