其他分享
首页 > 其他分享> > SSM(SpringMVC+Spring+Mybatis)整合

SSM(SpringMVC+Spring+Mybatis)整合

作者:互联网

一、导包:   <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-context</artifactId>     <version>5.1.3.RELEASE</version> </dependency>        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-core</artifactId>     <version>5.1.3.RELEASE</version> </dependency>       <!-- https://mvnrepository.com/artifact/org.springframework/spring-web --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-web</artifactId>     <version>5.1.3.RELEASE</version> </dependency>       <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-webmvc</artifactId>     <version>5.1.3.RELEASE</version> </dependency>     <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-orm</artifactId>     <version>5.1.3.RELEASE</version> </dependency>       <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis-spring</artifactId>     <version>1.3.1</version> </dependency>    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency>     <groupId>org.mybatis</groupId>     <artifactId>mybatis</artifactId>     <version>3.4.6</version> </dependency>    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency>     <groupId>org.aspectj</groupId>     <artifactId>aspectjweaver</artifactId>     <version>1.9.2</version> </dependency>         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency>     <groupId>mysql</groupId>     <artifactId>mysql-connector-java</artifactId>     <version>5.1.41</version> </dependency>         <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency>     <groupId>com.mchange</groupId>     <artifactId>c3p0</artifactId>     <version>0.9.5.2</version> </dependency>    <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency>     <groupId>jstl</groupId>     <artifactId>jstl</artifactId>     <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard --> <dependency>     <groupId>taglibs</groupId>     <artifactId>standard</artifactId>     <version>1.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency>     <groupId>org.apache.logging.log4j</groupId>     <artifactId>log4j-core</artifactId>     <version>2.11.1</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency>     <groupId>log4j</groupId>     <artifactId>log4j</artifactId>     <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency>     <groupId>org.slf4j</groupId>     <artifactId>slf4j-log4j12</artifactId>     <version>1.7.25</version>     <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/asm/asm-commons --> <dependency>     <groupId>asm</groupId>     <artifactId>asm-commons</artifactId>     <version>3.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.javassist/javassist --> <dependency>     <groupId>org.javassist</groupId>     <artifactId>javassist</artifactId>     <version>3.17.1-GA</version> </dependency>   <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload --> <dependency>     <groupId>commons-fileupload</groupId>     <artifactId>commons-fileupload</artifactId>     <version>1.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-core</artifactId>     <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-databind</artifactId>     <version>2.9.8</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency>     <groupId>com.fasterxml.jackson.core</groupId>     <artifactId>jackson-annotations</artifactId>     <version>2.9.8</version> </dependency> 二、准备数据库表:     #创建数据库ssm        DROP DATABASE IF EXISTS ssm;        CREATE DATABASE ssm;        USE ssm;     #创建书本表        DROP TABLE IF EXISTS ssm_book;        CREATE TABLE ssm_book (          id INT(11) PRIMARY KEY AUTO_INCREMENT NOT NULL,          NAME VARCHAR(111),          detail VARCHAR(256),          userId INT(22)       );     INSERT INTO ssm_book(id, NAME,detail,userId) VALUES        (1,"javaWeb","JavaWeb从入门到精通",1),        (2,"PhotoShop","图片图像处理",1),        (3,"mysql从入门到精通","数据库专业书籍",2);    三、创建POJO:Book.java      public class Book implements Serializable {          private static final long serialVersionUID = 1L;          private Integer id;          private String name;          private String detail;          private Integer userId;       }     四、创建jdbc.properties文件       jdbc.driver=com.mysql.jdbc.Driver       jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8       jdbc.username=root       jdbc.password=admin   五、创建applicationContext.xml配置文件        <!-- 配置读取properties文件jdbc.properties以连接数据库 -->        <context:property-placeholder location="classpath:jdbc.properties"/>        <!-- 配置数据源-->        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">           <property name="driverClass" value="${jdbc.driver}"/>           <property name="jdbcUrl" value="${jdbc.url}"/>           <property name="user" value="${jdbc.username}"/>           <property name="password" value="${admin}"/>        </bean>       <!-- 配置sqlSessionFactory -->       <bean class="org.mybatis.spring.SqlSessionFactoryBean">         <property name="dataSource" ref="dataSource"/>         <property name="typeAliasesPackage" value="com.ssm.pojo"/>       </bean>       <!-- 配置mapper扫描器 -->       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">          <property name="basePackage" value="com.ssm.mapper"/>       </bean>       <!-- 配置service扫描器 -->         <context:component-scan base-package="com.ssm.service"/>       <!-- 配置事务-->       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">         <property name="dataSource" ref="dataSource"/>       </bean>       <!--配置事务-通知  -->       <tx:advice id="txAdvice" transaction-manager="transactionManager">         <tx:attributes>            <tx:method name="add*" propagation="REQUIRED"/>            <tx:method name="delete*" propagation="REQUIRED"/>            <tx:method name="find*" propagation="REQUIRED"/>            <tx:method name="edit*" propagation="REQUIRED"/>          </tx:attributes>      </tx:advice>      <!-- 配置事务-切面 -->       <aop:config>         <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ssm.service.*.*(..))"/>       </aop:config> 六、创建springmvc-config.xml文件      <!-- 配置Controller扫描 -->     <context:component-scan base-package="com.ssm.controller"/>     <!-- 配置注解驱动 -->     <mvc:annotation-driven/>     <!-- 配置静态资源响应 -->     <mvc:default-servlet-handler/>     <!-- 配置视图解析器 -->     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">       <property name="prefix" value="/WEB-INF/views/"/>       <property name="suffix" value=".jsp"/>     </bean>    七、配置web.xml     <!-- needed for ContextLoaderListener -->      <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>classpath:applicationContext.xml</param-value>      </context-param>      <!-- Bootstraps the root web application context before servlet initialization -->      <listener>           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>            <!-- The front controller of this Spring Web application, responsible for handling all application requests -->      <servlet>           <servlet-name>springDispatcherServlet</servlet-name>           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>           <init-param>               <param-name>contextConfigLocation</param-name>               <param-value>classpath:springmvc-config.xml</param-value>           </init-param>           <load-on-startup>1</load-on-startup>      </servlet>      <!-- Map all requests to the DispatcherServlet for handling -->      <servlet-mapping>           <servlet-name>springDispatcherServlet</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>         <init-param>             <param-name>forceEncoding</param-name>             <param-value>true</param-value>         </init-param>   </filter>   <filter-mapping>         <filter-name>characterEncodingFilter</filter-name>         <url-pattern>/*</url-pattern>   </filter-mapping>    八、创建映射Mapper:    BookMapper.java      public interface BookMapper {         public List<Book> findAll();       }     BookMapper.xml     <mapper namespace="com.ssm.mapper.BookMapper">        <select id="findAll" resultType="Book">          select * from ssm_book        </select>      </mapper>  九、创建服务层:        IBookService.java      public interface IBookService {         public List<Book> findAllBook();       }     BookServiceImpl.java     @Service     public class BookServiceImpl implements IBookService {      @Autowired      private BookMapper bookMapper;      @Override      public List<Book> findAllBook() {           return bookMapper.findAll();      }   } 十、创建控制器:BookController.java    @Controller   public class BookController {      @Autowired      private IBookService bookService;      @RequestMapping(value="/main")      public String Main(Model model) {           List<Book> books = bookService.findAllBook();           model.addAttribute("books",books);           return "main";       }    }  十一、创建视图:main.jsp      

标签:SpringMVC,artifact,SSM,mvnrepository,springframework,https,Mybatis,org,com
来源: https://www.cnblogs.com/lone5wolf/p/11065465.html