Springmvc learning notes (7)-springmvc integration of mybatis er
Springmvc learning notes (7)-springmvc integration of mybatis er
This document records the configuration of springmvc integrated dao.
Integrate dao
First, add two files in the resource Folder: the database configuration file and the log configuration file.
Database Configuration File db. properties
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://120.25.162.238:3306/mybatis001?characterEncoding=utf-8jdbc.username=rootjdbc.password=123
Log configuration file log4j. properties
# Global logging configurationlog4j.rootLogger=DEBUG, stdout# Console output...log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
SqlMapConfig. xml
Mybatis configuration file
Create a mybatis folder in the resources directory and create a sqlMapConfig. xml file.
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E--><configuration> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%85%A8%E5%B1%80setting%E9%85%8D%E7%BD%AE%EF%BC%8C%E6%A0%B9%E6%8D%AE%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E5%88%AB%E5%90%8D%20%2D%2D%3E--> <typealiases> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%89%B9%E9%87%8F%E6%89%AB%E6%8F%8F%E5%88%AB%E5%90%8D%20%2D%2D%3E--> <package name="com.iot.learnssm.firstssm.po"> </package></typealiases> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AEmapper%0A%20%20%20%20%E7%94%B1%E4%BA%8E%E4%BD%BF%E7%94%A8spring%E5%92%8Cmybatis%E7%9A%84%E6%95%B4%E5%90%88%E5%8C%85%E8%BF%9B%E8%A1%8Cmapper%E6%89%AB%E6%8F%8F%EF%BC%8C%E8%BF%99%E9%87%8C%E4%B8%8D%E9%9C%80%E8%A6%81%E9%85%8D%E7%BD%AE%E4%BA%86%E3%80%82%0A%20%20%20%20%E5%BF%85%E9%A1%BB%E9%81%B5%E5%BE%AA%EF%BC%9Amapper.xml%E5%92%8Cmapper.java%E6%96%87%E4%BB%B6%E5%90%8C%E5%90%8D%E4%B8%94%E5%9C%A8%E4%B8%80%E4%B8%AA%E7%9B%AE%E5%BD%95%0A%20%20%20%20%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%3Cmappers%3E%0A%0A%20%20%20%20%3C%2Fmappers%3E%20%2D%2D%3E--></configuration></code>
ApplicationContext-dao.xml
Create a spring folder under the resources directory and a new applicationContext-dao.xml File
Configuration:
Data Source SqlSessionFactory mapper Scanner
<code class="language-xml hljs "><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDdb.properties%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%86%85%E5%AE%B9%EF%BC%8Cdb.properties%E6%96%87%E4%BB%B6%E4%B8%ADkey%E5%91%BD%E5%90%8D%E8%A6%81%E6%9C%89%E4%B8%80%E5%AE%9A%E7%9A%84%E7%89%B9%E6%AE%8A%E8%A7%84%E5%88%99%20%2D%2D%3E--> <context:property-placeholder location="classpath:db.properties"> <!--{cke_protected}{C}%3C!%2D%2D%20%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90%20%EF%BC%8Cdbcp%20%2D%2D%3E--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}"> <property name="url" value="${jdbc.url}"> <property name="username" value="${jdbc.username}"> <property name="password" value="${jdbc.password}"> <property name="maxActive" value="30"> <property name="maxIdle" value="5"> </property></property></property></property></property></property></bean> <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BB%8E%E6%95%B4%E5%90%88%E5%8C%85%E9%87%8C%E6%89%BE%EF%BC%8Corg.mybatis%3Amybatis-spring%3A1.2.4%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20sqlSessionFactory%20%2D%2D%3E--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%20%2D%2D%3E--> <property name="dataSource" ref="dataSource"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%8A%A0%E8%BD%BDmybatis%E7%9A%84%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E--> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"> </property></property></bean> <!--{cke_protected}{C}%3C!%2D%2D%20mapper%E6%89%AB%E6%8F%8F%E5%99%A8%20%2D%2D%3E--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--{cke_protected}{C}%3C!%2D%2D%20%E6%89%AB%E6%8F%8F%E5%8C%85%E8%B7%AF%E5%BE%84%EF%BC%8C%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E6%89%AB%E6%8F%8F%E5%A4%9A%E4%B8%AA%E5%8C%85%EF%BC%8C%E4%B8%AD%E9%97%B4%E4%BD%BF%E7%94%A8%E5%8D%8A%E8%A7%92%E9%80%97%E5%8F%B7%E9%9A%94%E5%BC%80%20%2D%2D%3E--> <property name="basePackage" value="com.iot.learnssm.firstssm.mapper"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"> <!--{cke_protected}{C}%3C!%2D%2D%20%3Cproperty%20name%3D%22sqlSessionFactory%22%20ref%3D%22sqlSessionFactory%22%20%2F%3E%0A%20%20%20%20%20%20%20%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%95%B0%E6%8D%AE%E6%BA%90%E9%85%8D%E7%BD%AE%E4%B8%8D%E7%AE%A1%E7%94%A8%EF%BC%8C%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E4%B8%8D%E4%B8%8A%E3%80%82%0A%20%20%20%20%20%20%20%E4%B8%94spring%204%E5%BC%83%E7%94%A8%0A%20%20%20%20%20%20%20%2D%2D%3E--> </property></property></bean></context:property-placeholder></beans></code>
Reverse Engineering generates po class and mapper (adding, deleting, modifying, and querying a single table), and manually defines the product query mapper
Generally, associated queries are used to query mapper. We recommend that you customize mapper.
ItemsMapperCustom. xml
<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%20%3F%2D%2D%3E--><mapper namespace="com.iot.learnssm.firstssm.mapper.ItemsMapperCustom"> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%AE%9A%E4%B9%89%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E7%9A%84sql%E7%89%87%E6%AE%B5%EF%BC%8C%E5%B0%B1%E6%98%AF%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6%20%2D%2D%3E--> <sql id="query_items_where"> <!--{cke_protected}{C}%3C!%2D%2D%20%E4%BD%BF%E7%94%A8%E5%8A%A8%E6%80%81sql%EF%BC%8C%E9%80%9A%E8%BF%87if%E5%88%A4%E6%96%AD%EF%BC%8C%E6%BB%A1%E8%B6%B3%E6%9D%A1%E4%BB%B6%E8%BF%9B%E8%A1%8Csql%E6%8B%BC%E6%8E%A5%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%95%86%E5%93%81%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6%E9%80%9A%E8%BF%87ItemsQueryVo%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1%20%E4%B8%ADitemsCustom%E5%B1%9E%E6%80%A7%E4%BC%A0%E9%80%92%20%2D%2D%3E--> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemsCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%' </if> </if> </sql> <!--{cke_protected}{C}%3C!%2D%2D%20%E5%95%86%E5%93%81%E5%88%97%E8%A1%A8%E6%9F%A5%E8%AF%A2%20%2D%2D%3E--> <!--{cke_protected}{C}%3C!%2D%2D%20parameterType%E4%BC%A0%E5%85%A5%E5%8C%85%E8%A3%85%E5%AF%B9%E8%B1%A1(%E5%8C%85%E8%A3%85%E4%BA%86%E6%9F%A5%E8%AF%A2%E6%9D%A1%E4%BB%B6)%0A%20%20%20%20%20%20%20%20resultType%E5%BB%BA%E8%AE%AE%E4%BD%BF%E7%94%A8%E6%89%A9%E5%B1%95%E5%AF%B9%E8%B1%A1%0A%20%20%20%20%20%2D%2D%3E--> <select id="findItemsList" parametertype="com.iot.learnssm.firstssm.po.ItemsQueryVo" resulttype="com.iot.learnssm.firstssm.po.ItemsCustom"> SELECT items.* FROM items </select></mapper></code>
ItemsMapperCustom. java
Public interface ItemsMapperCustom {// List of item queries
FindItemsList (ItemsQueryVo itemsQueryVo) throws Exception ;}
Po type
ItemsCustom
Package com. iot. learnssm. firstssm. po;/*** Created by Brian on 2016/3/2. * extension class of product information */public class ItemsCustom extends Items {// Add extension attributes of product information}
Input pojo packaging class
Package com. iot. learnssm. firstssm. po;/*** Created by Brian on 2016/3/2. */public class ItemsQueryVo {// product information private Items items; // extended private ItemsCustom itemsCustom for the system scalability of the Original po; public Items getItems () {return items;} public void setItems (Items items) {this. items = items;} public ItemsCustom getItemsCustom () {return itemsCustom;} public void setItemsCustom (ItemsCustom itemsCustom) {this. itemsCustom = itemsCustom ;}}
Integrate the project directory after dao