SSM整合(实现简单查询功能)

在名为ssm的数据库内创建表

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

创建工程

SSM整合(实现简单查询功能)_第1张图片

pom.xml



    4.0.0

    com.by
    ssm
    1.0-SNAPSHOT
    war

    
        5.1.8.RELEASE
        1.6.6
        1.2.12
        5.1.47
        3.4.5
        1.1.0
    
    
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
            org.springframework
            spring-tx
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            com.alibaba
            druid
            1.1.0
        
		
        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            javax.servlet.jsp
            jsp-api
            2.0
            provided
        

        
            jstl
            jstl
            1.2
        

        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
                
    
    
        
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    
                    8080
                    
                    /
                    
                    UTF-8
                
            
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
        
        
            
                src/main/java
                
                    **/*.xml
                
            
            
                src/main/resources
            
        
    

log4j.properties

log4j.rootLogger=DEBUG,A1

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %c%l%m%n

 

db.properties

#写自己的数据库

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3305/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=

applicationContext-dao.xml






    


    
        
        
        
        
    


    

        
    
    
    
        
        
    

 

applicationContext-tx.xml



    

    
    
        
        
    

    


















 

applicationContext-service.xml



    
    

 

springmvc.xml





    
    

    
    

    
    
        
        
    

web.xml



    
    
        contextConfigLocation
        classpath:applicationContext-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

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


    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
        1
    
    
        springmvc
        
        /
    

pojo包

public class Account {
    private Integer id;
    private String name;
    private Double money;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

controller包

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @RequestMapping("/selectAccount")
    public String selectAccount(Model model){
        List list = accountService.selectAccount();
        model.addAttribute("list",list);
        return "select_account";
    }
}

service包

AccountService接口
public interface AccountService {
    List selectAccount();
}
AccountServiceImpl实现类
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public List selectAccount() {
        return accountMapper.selectAccount();
    }
}

mapper包

AccountMapper
public interface AccountMapper {
    List selectAccount();
}
AccountMapper.xml



    

select_account.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


    Title


查询所有账户

id name money
${list.id} ${list.name} ${list.money}

测试

部署项目,浏览器运行localhost:8080/account/selectAccount

SSM整合(实现简单查询功能)_第2张图片

你可能感兴趣的:(java,数据库,开发语言)