一、dispatcher-servlet.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"
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">
<!--此文件负责整个mvc中的配置-->
<!--启用spring的一些annotation -->
<context:annotation-config/>
<!-- 配置注解驱动 可以将request参数与绑定到controller参数上 -->
<mvc:annotation-driven/>
<!--静态资源映射-->
<!--本项目把静态资源放在了webapp的statics目录下,资源映射如下-->
<mvc:resources mapping="/css/**" location="/statics/css/"/>
<mvc:resources mapping="/js/**" location="/statics/js/"/>
<mvc:resources mapping="/image/**" location="/statics/images/"/>
<mvc:default-servlet-handler/>
<!--cors跨域支持-->
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="http://127.0.0.1:8020,http://127.0.0.1:8081,http://127.0.0.1:8000"
allowed-methods="GET,POST,PUT,DELETE,OPTIONS"
allowed-headers="Content-Type,token"
allow-credentials="true"
max-age="123" />
</mvc:cors>
<!--这句要加上,要不然可能会访问不到静态资源,具体作用自行百度-->
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/views/"/><!--设置JSP文件的目录位置-->
<property name="suffix" value=".jsp"/>
<property name="exposeContextBeansAsAttributes" value="true"/>
</bean>
<!-- 自动扫描装配 -->
<context:component-scan base-package="com.skyrity.controller">
<!--<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />-->
</context:component-scan>
</beans>
二、applicationContext.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.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 "
default-autowire="byName">
<!--开始注解扫描 希望处理service和到,controller不需要spring框架去处理-->
<context:component-scan base-package="com.skyrity">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--Spring整合MyBatis框架-->
<!-- 配置连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="jdbcUrl" value="jdbc:sqlserver://127.0.0.1:1433;databaseName=shop" />
<property name="user" value="sa" />
<property name="password" value="!Sky2004aaaaaa!"/>
<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="10000" />
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>
<!-- 配置SQL SessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Dao接口所在包-->
<bean id= "mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.skyrity.dao" />
</bean>
</beans>
三、Controller 类
@Controller
@RequestMapping("/api")
public class TestController {
private static Logger logger = Logger.getLogger(ShopController.class);
@Autowired
TestService testService; //需要调用Spring逻辑服务类
@RequestMapping(value="/login.do",method = RequestMethod.POST)
public void login(HttpServletRequest request, HttpServletResponse response) {
}
}
四、Service类
//定义Sevice接口
public interface TestService {
String login(HttpServletRequest request, String userName, String password);
}
//定义ServiceImpl实现类型
@Service("ShopService")
public class TestServiceImpl implements ShopService{
@Autowired
TestDao testDao; //mybatis 持久化服务接口
@Override
public String login(HttpServletRequest request, String userName, String password) {
}
五、myBatis持久化服务接口
@Repository
public interface TestDao {
//查询语句
@Select("select Top 1 * from Face_Register where telNo=#{telNo}")
Face_Register getByTelephone(String telNo);
//存储过程
@Select("{call dbo.Page(" +
"#{map.out_PageCount,mode=OUT,jdbcType=INTEGER},"+ //总页数输出
"#{map.out_ResultCount,mode=OUT,jdbcType=INTEGER},"+ //总记录数输出
"#{map.out_SQL,mode=OUT,jdbcType=NVARCHAR}," + //返回数据库SQL查询语句
"#{map.in_Table,mode=IN,jdbcType=NVARCHAR},"+ //查询表名
"#{map.in_Key,mode=IN,jdbcType=VARCHAR}," + //主键
"#{map.in_Fields,mode=IN,jdbcType=NVARCHAR}," + //查询字段
"#{map.in_Where,mode=IN,jdbcType=NVARCHAR}," + //查询条件
"#{map.in_Order,mode=IN,jdbcType=NVARCHAR}," + //排序字段
"#{map.in_Begin,mode=IN,jdbcType=INTEGER}," + //开始位置
"#{map.in_PageIndex,mode=IN,jdbcType=INTEGER}," + //当前页数
"#{map.in_PageSize,mode=IN,jdbcType=INTEGER})}") //页大小
@Options(statementType= StatementType.CALLABLE)
List<Face_Register> getByAttrs(@Param("map") Map<String,String> map);
//插入语句
@Insert("insert into Face_Register(name,openId,telNo,applyTime,state,imgUrl,cardNo) " +"values(#{faceRegister.name},#{faceRegister.openId},#{faceRegister.telNo},getDate(),#{faceRegister.state}," +"#{faceRegister.imgUrl},#{faceRegister.cardNo})")
long add(@Param("faceRegister") Face_Register faceRegister);
//修改语句
@Update("update Face_Register set state=#{state} where id=#{id} and state<>2")
long approve(@Param("id") int id,@Param("state") int state);
}