当前位置:   article > 正文

springmvc+mybatis创建_创建一个java web项目,以spring mvc整合mybatis框架,实现用户注册功能。 实验

创建一个java web项目,以spring mvc整合mybatis框架,实现用户注册功能。 实验代码

1. 利用idea生成maven的springmvc

2.创建java、resource目录

3 pom文件如下

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.7</maven.compiler.source>
  4. <maven.compiler.target>1.7</maven.compiler.target>
  5. <mybatis.version>3.4.1</mybatis.version>
  6. <mybatis.spring.version>1.3.1</mybatis.spring.version>
  7. <mysql.version>5.1.38</mysql.version>
  8. </properties>
  9. <dependencies>
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.11</version>
  14. <scope>test</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-core</artifactId>
  19. <version>4.2.6.RELEASE</version>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework</groupId>
  23. <artifactId>spring-beans</artifactId>
  24. <version>4.2.6.RELEASE</version>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework</groupId>
  28. <artifactId>spring-aspects</artifactId>
  29. <version>4.2.6.RELEASE</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-webmvc</artifactId>
  34. <version>4.2.6.RELEASE</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework</groupId>
  38. <artifactId>spring-context</artifactId>
  39. <version>4.2.6.RELEASE</version>
  40. </dependency>
  41. <dependency>
  42. <groupId>com.alibaba</groupId>
  43. <artifactId>fastjson</artifactId>
  44. <version>1.2.46</version>
  45. </dependency>
  46. <dependency>
  47. <groupId>com.fasterxml.jackson.core</groupId>
  48. <artifactId>jackson-core</artifactId>
  49. <version>2.7.0</version>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.fasterxml.jackson.core</groupId>
  53. <artifactId>jackson-databind</artifactId>
  54. <version>2.7.0</version>
  55. </dependency>
  56. <dependency>
  57. <groupId>org.mybatis</groupId>
  58. <artifactId>mybatis</artifactId>
  59. <version>${mybatis.version}</version>
  60. </dependency>
  61. <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
  62. <dependency>
  63. <groupId>org.mybatis</groupId>
  64. <artifactId>mybatis-spring</artifactId>
  65. <version>${mybatis.spring.version}</version>
  66. </dependency>
  67. <!-- mysql -->
  68. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  69. <dependency>
  70. <groupId>mysql</groupId>
  71. <artifactId>mysql-connector-java</artifactId>
  72. <version>${mysql.version}</version>
  73. </dependency>
  74. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  75. <dependency>
  76. <groupId>com.alibaba</groupId>
  77. <artifactId>druid</artifactId>
  78. <version>1.1.12</version>
  79. </dependency>
  80. <dependency>
  81. <groupId>org.springframework</groupId>
  82. <artifactId>spring-jdbc</artifactId>
  83. <version>4.2.6.RELEASE</version>
  84. </dependency>
  85. </dependencies>

4 web.xml如下

  1. <servlet>
  2. <servlet-name>springmc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:applicationContext.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>springmc</servlet-name>
  12. <url-pattern>/</url-pattern>
  13. </servlet-mapping>

5. applicationContext.xml 

  1. ?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <context:component-scan base-package="com.xiaoding"/>
  10. <bean
  11. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  12. <property name="messageConverters">
  13. <list>
  14. <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
  15. <!-- <ref bean="mappingJacksonHttpMessageConverter" />-->
  16. </list>
  17. </property>
  18. </bean>
  19. <import resource="application-dao.xml"/>
  20. </beans>

6. application-dao.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  10. <!-- 基本属性 url,user,pass -->
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  12. <property name="url" value="jdbc:mysql://localhost:3306/bank?characterEncoding=utf-8"/>
  13. <property name="username" value="root"/>
  14. <property name="password" value="053100"/>
  15. </bean>
  16. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  17. <property name="dataSource" ref="dataSource"/>
  18. <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
  19. <property name="typeAliasesPackage" value="com.xiaoding.domain"></property>
  20. </bean>
  21. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  22. <property name="basePackage" value="com.xiaoding.mapper"/>
  23. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  24. </bean>
  25. </beans>

7.mapper配置如下:

  1. <!DOCTYPE mapper
  2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.xiaoding.mapper.StudentMapper">
  5. <select id="getStudents" resultType="Student">
  6. select * from student
  7. </select>
  8. </mapper>

8.整体文件目录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/692945
推荐阅读
相关标签
  

闽ICP备14008679号