• SSM框架整合


    SSM整合

    导入jar包

    spring相关jar包

    spring-aop-4.3.24.RELEASE.jar
    spring-aspects-4.3.24.RELEASE.jar
    spring-beans-4.3.24.RELEASE.jar
    spring-context-4.3.24.RELEASE.jar
    spring-context-support-4.3.24.RELEASE.jar
    spring-core-4.3.24.RELEASE.jar
    spring-expression-4.3.24.RELEASE.jar
    spring-jdbc-4.3.24.RELEASE.jar
    spring-orm-4.3.24.RELEASE.jar
    spring-oxm-4.3.24.RELEASE.jar
    spring-tx-4.3.24.RELEASE.jar
    spring-web-4.3.24.RELEASE.jar
    spring-webmvc-4.3.24.RELEASE.jar
    aspectjweaver.jar
    

    mybatis相关jar包

    mybatis-3.5.2.jar
    

    spring和mybatis的整合包

    mybatis-spring-1.3.3.jar
    

    jdbc数据包

    mysql-connector-java-5.1.47.jar
    

    日志包

    commons-logging-1.2.jar
    log4j-1.2.17.jar
    log4j-api-2.11.2.jar
    log4j-core-2.11.2.jar
    

    分页插件:PageHelper

    jsqlparser-2.1.jar
    pagehelper-5.1.10.jar
    

    druid连接池包

    druid-1.1.20.jar
    

    配置文件

    数据库配置文件

    jdbc.properties

    # 一般properties的第一行不要书写相关配置  可能不生效
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/0812_stmng?useUnicode=true&characterEncoding=utf8&useSSL=true
    jdbc.username=root
    jdbc.password=root
    

    日志配置文件

    log4j.properties

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # MyBatis logging configuration...
    log4j.logger.org.mybatis.example.BlogMapper=TRACE
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    

    spring配置文件

    application.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: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/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
    	<!-- 引入jdbc配置  -->
    	<context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="FALLBACK"/>
    	<!-- 配置数据源  整合 druid -->
    	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    		<property name="driverClassName" value="${jdbc.driver}"></property>
    		<property name="url" value="${jdbc.url}"></property>
    		<property name="username" value="${jdbc.username}"></property>
    		<property name="password" value="${jdbc.password}"></property>
    	</bean>
        
    	<!-- 配置SqlSessionFactoryBean -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 配置数据源 -->
    		<property name="dataSource" ref="dataSource"></property>
    		<!-- 配置插件 -->
    		<property name="plugins" >
    			<array>
    				<bean class="com.github.pagehelper.PageInterceptor"></bean>
    			</array>
    		</property>
    		<!-- 配置包下类别名 -->
    		<property name="typeAliasesPackage" value="com.sxt.pojo"></property>
    		<!-- 配置mapper地址 -->
    		<property name="mapperLocations" >
    			<array>
    				<value>classpath*:mapper/*.xml</value>
    			</array>
    		</property>
    	</bean>
    	<!-- 配置mapper接口代理类  mapper 扫描-->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    		<property name="basePackage" value="com.sxt.mapper"></property>
    	</bean>
    	
    	<!-- 配置事务 -->
    	<!-- 事务管理器对象 -->
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	<!-- 事务增强器 -->
    	<tx:advice id="txAdvice" transaction-manager="transactionManager">
    		<!-- 设置需要增强方法 -->
    		<tx:attributes>
    			<tx:method name="update*" />
    			<tx:method name="delete*" />
    			<tx:method name="insert*" />
    			<tx:method name="select*" read-only="true"/>
    			<tx:method name="*" read-only="true"/>
    		</tx:attributes>
    	</tx:advice>
        
    	<!-- 配置切面 -->
    	<aop:config>
    		<aop:pointcut expression="execution(* com.sxt.service.impl.*.*(..))" id="pc"/>
    		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    	</aop:config>
    	 <!-- 开启组件扫描 -->
    	<context:component-scan base-package="com.sxt"></context:component-scan>
    </beans>
    

    springmvc配置文件

    <?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:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
    		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
    	<!-- 开启mvc注解 -->
    	<mvc:annotation-driven />
    	<context:component-scan base-package="com.sxt.controller"></context:component-scan>
    	<!-- 配置视图解析器 -->
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/jsp"></property>
    	</bean>
    </beans>
    

    映射文件

    UserMapper.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">
     <!-- orm 映射文件  -->
    <mapper namespace="com.sxt.mapper.UserMapper">
    	<select id="selectAll" resultType="User">
    		select id as id,user_name as userName,password as password,real_name as realName from user
    	</select>
    </mapper>
    

    Java类

    pojo

    package com.sxt.pojo;
    
    public class User {
    	
    	private Integer id;
    	private String userName;	
    	private String password;	
    	private String realName;
        
    	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;
    	}
    	public String getRealName() {
    		return realName;
    	}
    	public void setRealName(String realName) {
    		this.realName = realName;
    	}
    
    	@Override
    	public String toString() {
    		return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", realName=" + realName + "]";
    	}
    
    }
    

    mapper

    package com.sxt.mapper;
    
    import java.util.List;
    
    import com.sxt.pojo.User;
    
    public interface UserMapper {
    
    	public List<User> selectAll();
        
    }
    

    service

    package com.sxt.service;
    
    import java.util.List;
    
    import com.sxt.pojo.User;
    
    public interface IUserService {
    
    	public List<User> queryAll();
        
    }
    
    package com.sxt.service.impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.sxt.mapper.UserMapper;
    import com.sxt.pojo.User;
    import com.sxt.service.IUserService;
    
    @Service
    public class UserServiceImpl implements IUserService {
        
    	@Autowired
    	private UserMapper userMapper;
    
    	@Override
    	public List<User> queryAll() {
    		return userMapper.selectAll();
    	}
    
    }
    

    controller

    package com.sxt.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.sxt.pojo.User;
    import com.sxt.service.IUserService;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
    	
    	@Autowired
    	private IUserService userService;
    	
    	@RequestMapping("/list.do")
    	public String list(Model model) {
    		List<User> users = userService.queryAll();
    		for (User user : users) {
    			System.out.println(user);
    		}
    		model.addAttribute("users", users);
    		return "/list.jsp";
    	}
    
    }
    
  • 相关阅读:
    [LeetCode] Permutation Sequence, Solution
    [LeetCode] Spiral Matrix II, Solution
    同学你好, google到你的帖子,想问下为什么你的解法不用考虑到2个数相同的情况?比如4,4,8...
    [LeetCode] Regular Expression Matching, Solution
    {4,4,8}倒还好,不过{4,8}就会有问题了。这里倒是一个bug。一个可用的办法就是用一个vis...
    [LeetCode] Merge Intervals, Solution
    请问这个解法的时间复杂度怎么分析?谢谢!
    未做题
    dude, visited[i 1] == 0 should be visited[i 1]...
    发布一个开源的c++网络事件库【转载Zark@cppthinker.com】
  • 原文地址:https://www.cnblogs.com/lyang-a/p/12171315.html
Copyright © 2020-2023  润新知