`
依旧熊孩子
  • 浏览: 8643 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

SpringMVC+Spring+MyBatis+MySql环境搭建

阅读更多

1、配置web.xml文件

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <!-- 加载spring容器 -->
  <listener>
      <display-name>Spring</display-name>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 
  <!-- 配置spring -->
  <context-param>
      <description>Spring</description>
    <param-name>contextConfigLocation</param-name>

    <!-- 根据自己文件路径配置 -->
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
 
  <!-- 配置springmvc -->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>

      <!-- 根据自己文件路径配置 -->
      <param-value>classpath:springmvc/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 
  <!-- 解决工程编码过滤器 -->
  <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>

 

2、配置springmvc.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-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
   
    <!-- 配置Spring MVC注解驱动 -->
    <mvc:annotation-driven/>
   
    <!-- 自动扫描Controller和Service -->

    <!-- 根据自己类文件包名称配置 -->
    <context:component-scan base-package="com.weibo.controller,com.weibo.service" />
   
    <!-- 配置静态资源,用以加载html,css,js,图片文件 -->
    <mvc:default-servlet-handler />
   
    <!-- 配置一个springmvc框架的视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 通过setter方法注入前缀 -->
        <property name="prefix" value="/html/" />
        <!-- 通过setter方法注入后缀 -->
        <property name="suffix" value=".html" />
    </bean>
   
    <!-- 支持文件上传
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
     -->
</beans>

 

3、配置applicationContext.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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> 
           
    <!-- 分解配置 jdbc.properites -->
    <context:property-placeholder location="classpath:jdbc/jdbc.properties" />
   
    <!-- 数据源c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxPoolSize" value="${c3p0.pool.size.max}" />
        <property name="minPoolSize" value="${c3p0.pool.size.min}" />
        <property name="initialPoolSize" value="${c3p0.pool.size.ini}" />
        <property name="acquireIncrement" value="${c3p0.pool.size.increment}" />
    </bean>
   
    <!-- sessionFactory 将spring和mybatis整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />    <!-- 加载mapper文件 -->
    </bean>
   
    <!-- mapper接口不能直接被注入到Spring当中,因此在此处进行扫描注入 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="basePackage" value="com.weibo.mapper"></property> 
    </bean>
   
    <!-- 事务 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="select*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="find" read-only="true" />
            <tx:method name="get" read-only="true" />
            <tx:method name="*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
   
   
    <!-- 面向切面编程 -->
    <aop:config>
        <aop:pointcut expression="execution(* service.*.*(..))" id="pointCut" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut" />
    </aop:config>
   
</beans>     

 

4、配置mybatis的sqlMapConfig.xml文件

 

<?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>
    <!-- 定义别名 -->
    <typeAliases>
        <typeAlias type="com.weibo.model.User" alias="User"/>
    </typeAliases>
    <!-- 这里不需要再注册mybatis的SQL映射文件 ,因为在Spring的配置文件中配置sqlSessionFactory时注册了mapper的路径-->
</configuration>    

 

5、定义数据库连接文件jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
c3p0.pool.size.max=20
c3p0.pool.size.min=5
c3p0.pool.size.ini=3
c3p0.pool.size.increment=2

 

6、定义mapper文件User.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.weibo.mapper.UserMapper">
    <select id="login" parameterType="User" resultType="User">
        select id from t_user where username = #{username} and password = #{password}
    </select>

</mapper>

 

7、测试

 

@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private UserService userService ;
   
    /**
     * 用户登录接口
     * @param user
     * @return
     */
    @RequestMapping(value="login",method=RequestMethod.POST)
    @ResponseBody
    public Json login(User user){
        Json json = new Json() ;
       
        try{
            User userResult = userService.login(user) ;
           
            if(userResult!=null){
                json.setBool(true) ;
                json.setMsg("登陆成功") ;
            }else{
                json.setBool(false) ;
                json.setMsg("用户名或密码错误") ;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
       
        return json ;
    }
}

@Service
public class UserService {
   
    @Autowired
    private UserMapper userDao ;
   
    public User login(User user)throws Exception{
        return userDao.login(user) ;
    }
}

public interface UserMapper{
   
    public User login(User user)throws Exception ;
}

public class User {
    private Integer id ;
    private String username ;       //用户名称
    private String password ;       //用户密码
   
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

0
0
分享到:
评论

相关推荐

    eclipse+maven+springmvc+spring+mybatis案例附带mysql数据库

    【标题】"eclipse+maven+springmvc+spring+mybatis案例附带mysql数据库"是一个典型的Java Web开发项目,它涵盖了多个关键的技术栈,包括Eclipse IDE、Maven构建工具、Spring MVC作为MVC框架、Spring核心框架以及...

    ZooKeeper+dubbo+springMvc+Mybatis+Mysql 例子

    ZooKeeper+dubbo+springMvc+Mybatis+Mysql实例,项目是由maven搭建的 整合Dubbo\spring\springMvc\Mybatis,整个压缩包中有两个项目分别是提供者和消费,启动方式是打成WAR形式放到tomcat中启动。

    Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境

    Maven+Spring+Spring MVC+MyBatis+MySQL,搭建SSM框架环境

    SSM框架(IDEA+Spring+SpringMVC+Maven+Mybatis+MySQL).zip

    SSM框架是Java Web开发中常用的三大框架——Spring、SpringMVC和Mybatis的组合,它们协同工作,构建高效、灵活的Web应用。IDEA作为Java开发的主流集成开发环境,Maven则作为项目管理和构建工具,而MySQL是常见的关系...

    基于SpringMVC+Spring+MyBatis个人技术博客系统源码.zip

    基于SpringMVC+Spring+MyBatis个人技术博客系统源码.zip 完整代码,可运行 项目描述 基于SSM实现的一个个人博客系统,适合初学SSM和个人博客制作的同学学习。有了这个源码,直接买了阿里云或腾讯服务器,就可以部署...

    maven+spring+springMVC+mybatis

    maven+spring+springMVC+mybatis 框架搭建 Maven 是一个优秀的项目管理和构建工具,Spring 是一个广泛使用的 Java 框架,SpringMVC 是基于 Spring 的一个 Web 框架,MyBatis 是一个持久层框架。在这个项目中,我们...

    SpringMvc+Spring+Mybatis+Maven+注解方式=整合

    "SpringMvc+Spring+Mybatis+Maven+注解方式"是一个经典的Java后端技术栈,它整合了四个关键组件,为开发人员提供了强大的工具和框架支持。下面将详细讲解这四个组件及其整合方式。 1. **Spring Framework**: ...

    基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统.zip

    基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统 基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统 基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统 基于SSM(springmvc+spring+mybatis)+...

    Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven实现的通用权限管理系统

    Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...

    maven+springMVC+mybatis+velocity+mysql+junit项目框架搭建

    本项目框架“maven+springMVC+mybatis+velocity+mysql+junit”提供了一种高效、灵活且可维护的解决方案。以下将详细讲解这些组件及其作用。 1. Maven: Maven是一个项目管理工具,用于构建、依赖管理和项目信息...

    基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统源码.zip

    基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统源码.zip 基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统源码.zip 基于SSM(springmvc+spring+mybatis)+Mysql图书管理系统源码.zip 基于SSM...

    maven+springmvc+redis+mybatis整合

    本项目以“maven+springmvc+redis+mybatis整合”为主题,旨在提供一个基于这些技术的集成框架,特别强调了利用Redis作为缓存来提升应用性能。下面将详细阐述这个框架中的各个组成部分以及它们之间的协作。 首先,...

    Maven搭建Spring+SpringMVC+Mybatis+MySql+SpringSecurity项目源码

    【标题】:“Maven搭建Spring+SpringMVC+Mybatis+MySql+SpringSecurity项目源码” 本项目源码展示了如何使用Maven构建一个完整的Java Web应用,它整合了Spring框架、SpringMVC、Mybatis、MySql数据库以及Spring ...

    springMVC+Spring+Mybatis+Maven整合代码案例

    4、搭建spring和mybatis的配置:spring-mybatis.xm(扫描dao)) 5、测试Spring+mybatis的框架搭建,写单元测试JUnit,测试事务配置等:model--&gt;dao(mapper)--&gt;service--&gt;test 6、映入SpringMVC:配置...

    基于SpringMvc+Spring+MyBatis的OA系统项目

    系统后端基于SpringMVC+Spring+Hibernate框架,前端页面采用JQuery+Bootstrap等主流技术; 流程引擎基于Snaker工作流;表单设计器基于雷劈网WEB表单设计器。 系统主要功能有: &gt;1.系统管理 &gt;&gt;系统管理包含有:基础...

    Springmvc+dubbo+mybatis+mysql+redis

    标题 "Springmvc+dubbo+mybatis+mysql+redis" 描述了一个基于Java技术栈的分布式微服务架构。在这个系统中,SpringMVC作为前端控制器处理HTTP请求,Dubbo用于服务治理,MyBatis是持久层框架,MySQL是关系型数据库,...

    spring+springmvc+mybatis搭建的一个酒店管理系统附带mysql数据库

    【标题】"spring+springmvc+mybatis搭建的酒店管理系统附带mysql数据库"涉及的核心技术是Java企业级开发中的Spring框架、SpringMVC模块以及MyBatis持久层框架,同时结合了MySQL数据库来存储和管理数据。这个项目是...

    基于Springboot+Mybatis+ SpringMvc+springsecrity+Redis完整网站后台管理系统

    项目描述 说明: spring security 全注解式的权限管理 动态配置权限,角色和资源,权限控制到...Springboot+Mybatis+ SpringMvc+springsecrity+Redis+bootstrap+jquery 数据库文件 压缩包内 jar包文件 maven搭建

    idea+spring+springmvc+mybatis

    总结起来,"idea+spring+springmvc+mybatis"代表了一种高效的Java Web开发解决方案,结合MySQL数据库,能够快速构建稳定、可扩展的企业级应用。对于初学者或者已经有一段时间没有接触编码的开发者来说,重新熟悉这个...

Global site tag (gtag.js) - Google Analytics