基于注解的mybatis整合spring项目html
采用了spring-boot,mybatis,freemarker等框架java
spring-boot:spirng的快速开发框架,继承了经常使用的spring库,且有内置tomcat方便调试。mysql
mybatis:方便快捷的ORM框架,能够省去编写dao层代码的麻烦。相比hibernate更为轻量级和易于学习。web
freemarker:页面模板框架,对于动态渲染页面有很大帮助,尤为对于填写数据等能够下降不少工做量。spring
gradle依赖配置以下:sql
compile( 'org.springframework.boot:spring-boot-starter-web:1.3.2.RELEASE', "org.springframework.boot:spring-boot-starter-web:1.3.2.RELEASE", "org.springframework.boot:spring-boot-starter-aop:1.3.2.RELEASE", "org.springframework.boot:spring-boot-starter-test:1.3.2.RELEASE", "org.springframework.boot:spring-boot-starter-jdbc:1.3.2.RELEASE", 'org.springframework:spring-context-support:4.2.5.RELEASE', 'com.alibaba:druid:1.0.16', 'mysql:mysql-connector-java:5.1.36', 'org.mybatis:mybatis:3.3.1', 'org.mybatis:mybatis-spring:1.2.4', 'org.freemarker:freemarker:2.3.23' ) }
在spring配置中,注解扫描、数据源配置等略过,关于mybatis的数据源配置以下:数组
<!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 实例化sqlSessionFactory时须要使用上述配置好的数据源以及SQL映射文件 --> <property name="dataSource" ref="dataSource" /> <!--使用注解配置sql,能够不用xml文件配置--> <!--<property name="mapperLocations" value="classpath:/mybatis/*.xml" />--> </bean> <!-- 配置扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描这个包以及它的子包下的全部映射接口类 --> <property name="basePackage" value="com.xinou.thinkcol.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
配置了扫描器以后,就能够自动扫描dao接口,没必要再另行声明。tomcat
dao接口代码以下:mybatis
public interface NewsDao { @Insert("insert into news( id,title,time,content) VALUES (#{id},#{title},#{time},#{content})") void insert(NewsBean ab); @Select("select a.* from news a order by time limit #{0},#{1}") List<NewsBean> pageQuery(int skip, int size); @Select("select * from news where id=#{0}") NewsBean find(String id); @Delete("delete from news where id=#{0}") void delete(String id); }
|
注解中声明sql语句,就能够省去编写配置文件的繁琐工做,也更便于管理和阅读。app
关于freemarker,须要声明viewResolver
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="suffix" value=".html"/> <property name="contentType" value="text/html;charset=UTF-8"/> </bean> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPath" value="/resource/" /> <property name="freemarkerSettings"> <props> <prop key="template_update_delay">0</prop> <prop key="default_encoding">UTF-8</prop> <prop key="number_format">0.##########</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="classic_compatible">true</prop> <prop key="template_exception_handler">ignore</prop> </props> </property> </bean>
|
能够看到将资源文件均放在了/resource/目录中。后缀仍旧使用.html,如果有部分html不须要通过模板,也能够将后缀名改为.fmt之类
而后就能够在html文件中使用freemarker模板。
关于向模板中添加数据:
@RequestMapping(value = "listActivity") public String sqhd(@RequestParam(required = false,defaultValue = "0") int page,ModelMap modelMap){ List<ActivityBean> list = activityService.activityPageQuery(page); modelMap.addAttribute("activitys",list); return "sqhd"; }
|
在modelmap中添加数据便可,在页面中使用${activitys.***}便可访问。对于数组须要按照数组访问