这是一个完整的Spring Mvc 站点
接下来要配置Spring+Mybatis
已有数据库:mysql sketchtouchdb 有表:
按以下文件目录新增相关文件:
1.新增映射实体
package dao.entities;
public class MiniNewsEntity {
private int id;
private byte announceDegree;
private String region;
private String content;
private String picUrls;
private String createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPicUrls() {
return picUrls;
}
public void setPicUrls(String picUrls) {
this.picUrls = picUrls;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
public byte getAnnounceDegree() {
return announceDegree;
}
public void setAnnounceDegree(byte announceDegree) {
this.announceDegree = announceDegree;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}
新增Mapper数据库方法接口
package dao.mappers;
import dao.entities.MiniNewsEntity;
public interface MiniNewsMapper {
public MiniNewsEntity get(int id);
}
在resources 中添加一个mybatis文件夹并新建 MiniNewsMapper.xml文件,内容如下:
这是查询数据库的Sql ,namespace与上面的Mapper接口相对应, id要与MiniNewsMapper.java 的方法名对应,resultType 输出参数也要对应.
再新增一个服务MiniNewsService 来调用:
package com.springmvcdemo.services;
import com.springmvcdemo.dao.entities.MiniNewsEntity;
import com.springmvcdemo.dao.mappers.MiniNewsMapper;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class MiniNewsService {
@Autowired
private MiniNewsMapper navRelDao;
public MiniNewsEntity get() {
return navRelDao.get(1);
}
}
最后在maincontroler 中添加一个调用该service的接口:
接下来就要与Spring扫描并载入这些部件了:
1.在resources目录新建数据库连接配置文件jdbc.properties:
内容如下:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sketchtouchdb
jdbc.username=root
jdbc.password=(数据库密码)
2.新增数据库映射文件applicationContext.dao.mybatis.xml
内容如下:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<typeAlias alias="MiniNewsEntity" type="dao.entities.MinNewsEntity"/>
</typeAliases>
<mappers>
<mapper resource="mybatis/MiniNewsMapper.xml"/>
</mappers>
</configuration>
注意路径
3.新增Spring 的dao层mapper接口扫描实例化与数据源配置 applicationContext-dao.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载数据库连接的资源文件 -->
<!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据源 dbcp数据库连接池 -->
<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}"/>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 加载Mybatis全局配置文件 -->
<property name="configLocation" value="classpath:applicationContext.dao.mybatis.xml"/>
</bean>
<!-- 配置mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
<property name="basePackage" value="com.springmvcdemo.dao.mappers"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
4.新增Spring 扫描实例化配置文件 applicationContext-spring.xml
这里要注意:
这需要配置Spring 的扫描顶级包名,当前有4个包,不好操作,所以决定用一个包去包住它:
调整后:
为什么不直接用com 而是使用com.springmvcdemo。因为如果直接使用com,配置扫描com包,则会导致fastjson出错。
故内容如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scanbase-package="com.springmvcdemo">
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
因为包的结构修改,故对应配置文件中的类型引用进行相应修改,如:
改为新的类路径
最后在web.xml中引入 applicationContext 相关配置文件,这里用到通配符*
在web-app节点下添加:
<!--加载spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
最后web.xml的完整配置为:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<!--配置前端控制器-->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--ContextconfigLocation配置springmvc加载的配置文件
适配器、处理映射器等
-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!--1、.action访问以.action结尾的由DispatcherServlet进行解析
2、/,所有访问都由DispatcherServlet进行解析
-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解决post乱码问题的过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
打包运行调试:
有问题查看日志
最后含Spring mybatis的接口运正常访问
至此:
.Net 码农搭使用 Intellij ideal 搭Java Maven + Spring + Spring MVC + Mybatis 框架 已配置完成
完整项目包下载:
坑:
1.在spring配置文件中直接扫描com会导致fastjson出错不可用
2.配置文件的路径问题:(暂时未了解原因)先记住经验
放在META-INF 下 classpath:META-INF/jdbc.properties 找不到
放出来:classpath:jdbc.properties 找得到
applicationContext.dao.mybatis.xml中定义
<configuration>
<propertiesresource="jdbc.properties"/>
<typeAliases>
<typeAliasalias="MinNewsEntity"type="com.springdemo.dal.entities.MinNewsEntity"/>
</typeAliases>
<mappers>
<mapperresource="mybatis/MinNewsMapper.xml"/> ---可读取
<mapperresource="classpath*:/mybatis/MinNewsMapper.xml"/> ---不可读取
</mappers>
</configuration>