SSM整合

项目结构

项目结构

基本配置

  1. 配置Jar包
    pom.xml

    4.0.0
    com.fyh.blog
    manager
    war
    1.0-SNAPSHOT
    manager Maven Webapp
    http://maven.apache.org
    
        utf-8
        1.8
        3.7.0
        4.3.14.RELEASE
        4.12
        3.4.6
        1.3.2
        5.1.38
        2.2.0
        3.1.0
        2.3.1
        1.2
    
    
        
            
                org.springframework
                spring-framework-bom
                ${spring-version}
                pom
                import
            
        
    
    
        
            junit
            junit
            ${junit-version}
            test
        
        
        
        
            org.springframework
            spring-webmvc
        
        
        
            org.springframework
            spring-test
        
        
        
            org.springframework
            spring-tx
        
        
        
            org.springframework
            spring-jdbc
        
        
        
            org.springframework
            spring-aspects
        
        
        
            org.apache.commons
            commons-dbcp2
            ${dbcp-version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis-version}
        
        
        
            org.mybatis
            mybatis-spring
            ${mybatis-spring-version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql-connector-version}
        
        
        
            javax.servlet
            javax.servlet-api
            ${servlet-version}
            provided
        
        
            javax.servlet
            jstl
            ${jstl-version}
            provided
        
        
            javax.servlet.jsp
            javax.servlet.jsp-api
            ${jsp-version}
            provided
        
    
    
        manager
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven-compailer-version}
                
                    ${project-encode}
                    ${jdk-version}
                    ${jdk-version}
                
            
        
    


  1. 配置日志
    log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.com.wu.dao=debug
log4j.logger.com.ibatis=debug 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 
log4j.logger.java.sql.ResultSet=debug 
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
######################################################################################
# Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
######################################################################################
# DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
log4j.logger.com.opensymphony.xwork2=error  
  1. 配置数据库
    resources/database/jdbc.properties
mysql.driverClass=com.mysql.jdbc.Driver
mysql.url=地址
mysql.user=用户名
mysql.password=密码

SSM整合

  1. Spring数据源配置
    resources/spring/spring-mybatis.xml


    
    
        
    
    
    
        
        
        
        
    

1 测试数据库连通性
test/java/common/TestDataBase.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring/spring-mybatis.xml")
public class TestDataBase {
    @Autowired
    @Qualifier("dataSource")
    private BasicDataSource dataSource;
  //测试数据库连通性
    @Test
    public void testDataBase() throws SQLException {
        //返回的是java.sql.Connection
        Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * from category");
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getInt("id"));
            System.out.println(resultSet.getString("kind"));
        }
    }
}

整合Spring和MyBatis

  1. Dao,Mappper,Domain
    com/fyh/blog/manager/login/dao/AccountDao.java
public interface AccountDao {
    Account selectByAccount(String account);
    int insertAccount(Account account);
}

com/fyh/blog/manager/login/domain/Account

@Alias("account") 
public class Account {
    private String account;
    private String password;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

/resources/mapper/AccountMapper.xml




    
    
        INSERT INTO account (account,password) VALUES (#{account},#{password})
    

  1. 整合配置
    resources/spring/spring-mybatis.xml
  
  
        
        
        
            
                com.fyh.blog.manager.login.domain.Account
            
        
    
     
    
        
        
    
  1. 测试MyBatis
    /test/com/fyh/blog/manager/login/dao/TestAccountDao.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring/spring-mybatis.xml")
public class TestAccountDao {
    @Autowired
    private AccountDao accountDao;

    @Test
    public void testMyBatis(){
        Account account =  accountDao.selectByAccount("admin");
        System.out.println(account.getAccount() + "," + account.getPassword());
    }
}

事务配置

  1. Spring配置文件
    resources/spring/spring-mybatis.xml
   
  
        
    
   
     
   
    
        
            
            
            
        
    
    
        
        
    
  1. service
    com/fyh/blog/manager/login/service/impl/LoginServiceImpl .java
@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    private AccountDao accountDao;

    public void insertAccount() throws Exception {
        Account account = new Account();
        account.setPassword("23");
        account.setAccount("123");
        accountDao.insertAccount(account);
        throw new Exception("出错");
    }
}
  1. 测试事务
    /test/com/fyh/blog/manager/login/service/TestLoginService.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring/spring-mybatis.xml")
public class TestLoginService {
    @Autowired
    private LoginService loginService;
    @Test
    public void insertAccount() throws Exception {
        loginService.insertAccount();
    }
}

spring-mvc配置

  1. 配置文件
    resources/spring-mvc/spring-mvc.xml


    
    
    
    
        
        
        
        
    

    
    

    
    
    
    
        
    

  1. controller
    /com/fyh/blog/manager/login/action/LoginAction.java
@Controller
public class LoginAction {
    @RequestMapping("dd")
    public String dd(Model model) {
        model.addAttribute("1", "2");
        return "dd";
    }
}
  1. 测试
    /test/com/fyh/blog/manager/login/action/TestLoginService.java
@RunWith(SpringJUnit4ClassRunner.class) //表示使用什么测试环境进行测试
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:/spring/spring-mybatis.xml", "classpath:/spring-mvc/spring-mvc.xml"})
public class TestLoginService {
    @Autowired
    WebApplicationContext webApplicationContext;
    //虚拟MVC请求,获取得到处理结果
    MockMvc mockMvc;

    @Before
    public void initMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void testPage() throws Exception {
       MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/dd")).andReturn();
        MockHttpServletRequest request = mvcResult.getRequest();
        String s = (String) request.getAttribute("1");
        assert s.equals("2");
    }
}

配置web.xml




    
    
        contextConfigLocation
        classpath:spring/spring-mybatis.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    

    
    
        tt
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc/spring-mvc.xml
        
        1
    
    
        tt
        /
    

首页

跳转成功

你可能感兴趣的:(SSM整合)