程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 第一次迭代,第一次

第一次迭代,第一次

編輯:JAVA綜合教程

第一次迭代,第一次


/ball/hnzj/controller/Chaxun.java
package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import domain.User;
import service.ChaxunService;

@Controller
@RequestMapping("/chaxun")
public class Chaxun {
    
    @Autowired
    private ChaxunService chaxunService;
    @RequestMapping("/chaxun")
    private String chaxun(User user){
    
        return "chaxun";        
    }
    
    @RequestMapping("/jieguo")
    public ModelAndView jieguo(User user){
    User user2 = chaxunService.getVs(user);
    
    ModelAndView mv =new ModelAndView();
    mv.setViewName("jieguo");
    mv.addObject("aa",user2);
        return mv;
        
        
    }
}


/ball/hnzj/dao/ChaxunDao.java
package dao;

import domain.User;

public interface ChaxunDao {
    public User getVs(User user);
    public User getDefen(User user);
}

/ball/hnzj/domain/User.java
package domain;

public class User {
    private int id;
    private String vs;
    private String defen;
    private String huoshengfang;
    
    
    
    
    public String getDefen() {
        return defen;
    }
    public void setDefen(String defen) {
        this.defen = defen;
    }
    public String getHuoshengfang() {
        return huoshengfang;
    }
    public void setHuoshengfang(String huoshengfang) {
        this.huoshengfang = huoshengfang;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getVs() {
        return vs;
    }
    public void setVs(String vs) {
        this.vs = vs;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", vs=" + vs + ", defen=" + defen + ", huoshengfang=" + huoshengfang + "]";
    }
    
}


/ball/hnzj/service/ChaxunService.java
package service;

import domain.User;

public interface ChaxunService {
    public User getVs(User user);
    public User getDefen(User user);
}


/ball/hnzj/service/Impl/ChaxunServiceImpl.java

package service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.ChaxunDao;
import domain.User;
import service.ChaxunService;


@Service
public class ChaxunServiceImpl implements ChaxunService{
    
    @Autowired
    private ChaxunDao chaxunDao ;

    @Override
    public User getVs(User user) {
        return chaxunDao.getVs(user);
    }

    @Override
    public User getDefen(User user) {
        return chaxunDao.getDefen(user);
    }
    
}


/ball/config/mybatis/mapper/ChaxunDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "http://mybatis.org/dtd/mybatis-3-mapper.dtd" "mybatis-3-mapper.dtd" >
<mapper  namespace="dao.ChaxunDao">
 
  <select id="getVs" parameterType="User" resultType="User" >     
      select * from ball where vs=#{vs};
    </select>
    <select id="getDefen" parameterType="User" resultType="User" >     
      select * from ball where defen=#{defen};
    </select>

</mapper>




/ball/config/mybatis/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "http://mybatis.org/dtd/mybatis-3-config.dtd" "mybatis-3-config.dtd" >
<configuration>

    <typeAliases>
        <typeAlias type="domain.User" alias="User"/>
    </typeAliases>
</configuration>



/ball/config/spring/applicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <context:annotation-config />
    <context:component-scan base-package="service"/>
    <!-- 加載數據庫配置文件jdbc.properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置數據庫連接池,使用dbcp連接池 -->
    <bean  id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        p:url="${jdbc.url}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}"
        p:driverClassName="${jdbc.driver}"
        p:maxActive="30"
        p:maxIdle="10"
        p:maxWait="1000"
        p:initialSize="15"/>
    <!-- 配置myBatis session工廠類,指定數據源,主配置文件,mapper映射文件,實體類所在包信息 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:configLocation="classpath:mybatis/config.xml"
        p:mapperLocations="classpath:mybatis/mapper/**/*.xml"/>
    <!-- 指定dao接口所在位置,該類會根據接口和mapper配置文件信息自動創建接口的實現類,service層直接使用即可 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="dao"/>
    <!-- 配置事務管理器 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource"/>
    <aop:config>
        <aop:pointcut expression="execution(* hnzj.*.service.**.*(..))" id="serviceMethods"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
    </aop:config>
    <!-- 把事務管理器包裝成advice -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save"  propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
</beans>



/ball/config/spring/spring-mvc.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <mvc:annotation-driven/>
    <!-- 掃描控制層 -->
    <context:component-scan base-package="controller" />
    <!-- 返回的邏輯視圖匹配到實際視圖 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/"
        p:suffix=".jsp"/>
    
</beans>


/ball/config/jdbc.properties
jdbc.username=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/login?useUnicode=true&amp;characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver


/ball/WebContent/WEB-INF/jsp/chaxun.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
    <style type="text/css">
    body, td
    {
      font-family: 宋體;
      font-size: 12px;
    }
    </style>
<script language="javascript" src="hpmenu.js"></script>
</head>
<body>
    <form name="form1" method="post" action="/ball/chaxun/jieguo">
        <table width="90%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
          <tr bgcolor="F1F1F1">
            <td height="24" colspan="2" align="center">戰隊比拼查詢表</td>
          </tr>
          <tr bgcolor="#FFFFFF">
            <td width="12%" height="24" align="center">所 在 洲:</td>
            <td><select name="vs" id="user"  >
                <option value="" selected>-----請選擇-----</option>
                    <option value='龍隊vs虎隊'>龍隊vs虎隊</option>
                    <option value='龍隊vs豹隊'>龍隊vs豹隊</option>
                    <option value='虎隊vs豹隊'>虎隊vs豹隊</option>
                    <option value='虎隊vs龍隊'>虎隊vs龍隊</option>
                    <option value='豹隊vs龍隊'>豹隊vs龍隊</option>
                    <option value='豹隊vs虎隊'>豹隊vs虎隊</option>
            </select></td>
          </tr>       
          <tr bgcolor="F1F1F1">
            <td height="24" colspan="2" align="center"> </td>
          </tr>
   </table>
     <br>
        <input type="submit" value="查詢" >
</form>
</body>
</html>


/ball/WebContent/WEB-INF/jsp/jieguo.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
${aa.vs} <br>比分 ${aa.defen}<br> 獲勝方 ${aa.huoshengfang}<br>

<a href="javascript :;" onClick="javascript :history.back(-1);">返回 </a>

</body>
</html>

/ball/WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ball</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
    <filter-name>characterFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>characterFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>ball</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ball</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

最近要開一個項目《考試系統》

用到ssm框架

Spring MVA       Spring       MyBatis

剛把框架學著搭好,老師就要求第一次迭代,學以致用,我便用ssm做了這個迭代。

在迭代的過程中 出現好多自己不理解的,多虧我們組長龔鵬飛和青鳥班王順利。還有陳老師,我簡直不能想想,昨天我還不理解需求書!!!

代碼較多,各個層,我也是昨天才搞明白。對不起我忘記加注釋了大家看不懂也正常,(好的編碼習慣還是要養成的)

雖然一個很小的功能,但麻雀雖小,五髒俱全。

還有裡面有些jar包,大家若想運行的話  請聯系我  我把jar包給你們

訪問頁面地址:http://localhost:8080/ball/chaxun/chaxun

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved