配置文件:mybatis 针对于映射文件
<?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>
<mappers>
<!--这是告诉Mybatis区哪找持久化类的映射文件,对于在src下的文件直接写文件名,
如果在某包下,则要写明路径,如:com.dao.CustomerDao-->
<mapper resource="com.dao.CustomerDao"></mapper>
</mappers>
</configuration>
springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/fonts/" mapping="/fonts/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:annotation-driven/>
<!--支持AOP注解支持,AOP底层使用代理技术
JDK动态代理,要求必须有接口
cglib代理,生成子类对象,proxy-target-class='true' 默认使用cglib的方式
-->
<aop:aspectj-autoproxy proxy-target-class="true" />
</beans>
application.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://mybatis.org/schema/mybatis-spring
http://mybtis.org/schema/mybatis-spring-1.2.xsd">
<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/boot_crm"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
</bean>
<!--mybatis 整合-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--获取数据源-->
<property name="dataSource" ref="dataSource"/>
<!--指定mybatis全局配置文件的位置-->
<property name="configLocation" value="classpath:mybatis-mvc.xml"/>
<!--指定mybatis,mapper文件的位置-->
<property name="mapperLocations" value="classpath*:/CustomerDao.xml"/>
</bean>
<!--配置扫描器,把 mybatis接口的实现加入到ioc容器中-->
<bean id="MapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入 sqlSessionFactory -->
<!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>-->
<!--扫描所有的 dao 接口的实现,加入到 ioc 容器中-->
<property name="basePackage" value="com.dao"></property>
</bean>
<!--事务控制-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制数据源-->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启注解的事务,也可以使用配置(XML)的方式控制-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
</beans>
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.dao.CustomerDao">
<!--public List<Customer> findAll();-->
<select id="findAll" parameterType="com.domain.Customer" resultType="com.domain.Customer">
select * from customer
</select>
<!--public List<Customer> findOne(int page,int num);-->
<select id="findOne" resultType="com.domain.Customer">
select * from customer
limit #{page},#{num}
</select>
<!--public Customer findCustomerByName(String name);-->
<select id="findCustomerByName" resultType="com.domain.Customer">
select * from customer where cust_name=#{name}
</select>
<!--public Customer findCustomerById(Integer id);-->
<select id="findCustomerById" resultType="com.domain.Customer">
select * from customer where cust_id=#{id}
</select>
<!--public Long insertCustomer(Customer customer);-->
<insert id="insertCustomer" parameterType="com.domain.Customer" >
insert into customer(
cust_name,
cust_linkman,
cust_phone,
cust_mobile,
cust_zipcode,
cust_address
)
values(
#{cust_name},
#{cust_linkman},
#{cust_phone},
#{cust_mobile},
#{cust_zipcode},
#{cust_address}
)
</insert>
<!--public Boolean updateCustomerById(Customer customer);-->
<update id="updateCustomerById" parameterType="com.domain.Customer" >
update customer
set
cust_name=#{cust_name},
cust_linkman=#{cust_linkman},
cust_phone=#{cust_phone},
cust_mobile=#{cust_mobile},
cust_zipcode=#{cust_zipcode},
cust_address=#{cust_address}
where cust_id=#{cust_id}
</update>
<!--public void deleteCustomerById(Integer id);-->
<delete id="deleteCustomerById">
delete from customer
where cust_id=#{id}
</delete>
</mapper>