一、
spring-mybatis.xml全局配置文件
二、mybatis.cfg.xml:核心映射文件配置
三、mapper.xml:SQL映射文件
四、配置jdbc.properties
------------------------------------------------------------
spring-mybatis.xml 全局配置文件--------------------------------------------------
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- datasource config-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
<context:component-scan base-package="com.communityhealth"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- datasource config-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.cfg.xml"/>
</bean>
<context:component-scan base-package="com.communityhealth"/>
</beans>
------------------------------------------------------------
mybatis.cfg.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>
<typeAliases><!-- 为实体类定义别名 -->
<package name="com.communityhealth.model"/>
</typeAliases>
<!-- SQL映射文件 -->
<mappers>
<mapper resource="com/communityhealth/mapper/OperatorMapper.xml" />
<mapper resource="com/communityhealth/mapper/SelectMapper.xml" />
<mapper resource="com/communityhealth/mapper/PatientMapper.xml" />
<mapper resource="com/communityhealth/mapper/TreatMapper.xml" />
</mappers>
</configuration>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases><!-- 为实体类定义别名 -->
<package name="com.communityhealth.model"/>
</typeAliases>
<!-- SQL映射文件 -->
<mappers>
<mapper resource="com/communityhealth/mapper/OperatorMapper.xml" />
<mapper resource="com/communityhealth/mapper/SelectMapper.xml" />
<mapper resource="com/communityhealth/mapper/PatientMapper.xml" />
<mapper resource="com/communityhealth/mapper/TreatMapper.xml" />
</mappers>
</configuration>
------------------------------------------------------------mapper.xml:SQL映射文件----------------------------------------------
<?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">
<!-- namespace表示当前所有语句都属于该空间下,防止不同xml内的语句有相同的ID -->
<mapper namespace="com.communityhealth.mapper.OperatorMapper">
<select id="getCzyxxInfo" parameterType="Operator" resultType="Operator">
select * from dbo.TB_DIC_CZYXX where XM=#{XM} and PASS=#{PASS} and RYLB=#{RYLB}
</select>
</mapper>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace表示当前所有语句都属于该空间下,防止不同xml内的语句有相同的ID -->
<mapper namespace="com.communityhealth.mapper.OperatorMapper">
<select id="getCzyxxInfo" parameterType="Operator" resultType="Operator">
select * from dbo.TB_DIC_CZYXX where XM=#{XM} and PASS=#{PASS} and RYLB=#{RYLB}
</select>
</mapper>
-----------mapper.xml:SQL映射文件 表列名和类属性不一样时--------
<resultMaptype="com.communityhealth.model.Classes" id="class">
<id property="cno"column="classno"/>
<result property="cname"column="className"/>
</resultMap>
<selectid="selectClasses01" parameterType="String" resultMap="class">
select className ,classno from dbo.classes where classNo = #{no}
</select>
------------------------------------------------------------jdbc.properties----------------------------------------------
driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433; DatabaseName=harmonyhis
jdbc.username=sa
jdbc.password=123
url=jdbc:sqlserver://localhost:1433; DatabaseName=harmonyhis
jdbc.username=sa
jdbc.password=123