課程專案《考試系統》首次迭代,使用 SSM (Spring MVC + Spring + MyBatis) 搭建基礎架構,實現戰隊比拼查詢功能。記錄完整代碼結構,作為從「會配置」到「能交付」的里程碑。
ball (Web 應用)
├── src
│ ├── controller/ChaxunController.java
│ ├── dao/ChaxunDao.java
│ ├── domain/User.java
│ ├── service/ChaxunService.java
│ └── service/impl/ChaxunServiceImpl.java
├── config
│ ├── mybatis/
│ │ ├── mapper/ChaxunDao.xml
│ │ └── config.xml
│ └── spring/
│ ├── applicationContext.xml
│ └── spring-mvc.xml
├── jdbc.properties
└── WebContent
├── WEB-INF
│ ├── jsp/chaxun.jsp, jieguo.jsp
│ └── web.xml
└── index.jsp (默認)
public class User {
private int id;
private String vs; // 對陣組合
private String defen; // 比分
private String huoshengfang; // 獲勝方
// Getter/Setter/toString 省略
}
public interface ChaxunDao {
User getVs(User user); // 根據對陣查詢
User getDefen(User user); // 根據比分查詢
}
<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>
// Service 介面
public interface ChaxunService {
User getVs(User user);
User getDefen(User user);
}
// 實現類
@Service
public class ChaxunServiceImpl implements ChaxunService {
@Autowired private ChaxunDao chaxunDao;
public User getVs(User user) { return chaxunDao.getVs(user); }
public User getDefen(User user) { return chaxunDao.getDefen(user); }
}
@Controller
@RequestMapping("/chaxun")
public class ChaxunController {
@Autowired private ChaxunService chaxunService;
@RequestMapping("/chaxun")
public String chaxun() { return "chaxun"; } // 轉發至查詢頁
@RequestMapping("/jieguo")
public ModelAndView jieguo(User user) {
User user2 = chaxunService.getVs(user);
ModelAndView mv = new ModelAndView("jieguo");
mv.addObject("aa", user2);
return mv;
}
}
<context:component-scan base-package="service"/>
<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" />
<!-- MyBatis SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:mybatis/config.xml"
p:mapperLocations="classpath:mybatis/mapper/**/*.xml"/>
<!-- Mapper 掃描器:自動創建 DAO 實現 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="dao"/>
<!-- 事務管理 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* hnzj.*.service.**.*(..))" id="serviceMethods"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<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"/>
chaxun.jsp:下拉選單選擇對陣組合,POST 提交至 /chaxun/jieguo
jieguo.jsp:EL 表達式顯示 ${aa.vs}、${aa.defen}、${aa.huoshengfang}
<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>
<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>
啟動 Tomcat,訪問:https://localhost:8080/ball/chaxun/chaxun
這是起點,下一個迭代見。