日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

一、搭建環境

1、創建數據庫表和表結構

create table account(

id INT identity(1,1) primary key,

name varchar(20),

[money] DECIMAL

2、創建maven的工程SSM,在pom.xml文件引入依賴

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.Apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.mingqi</groupId>

<artifactId>SSM</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>war</packaging>

<name>SSM Maven WebApp</name>

<!-- FIXME change it to the project's website -->

<url>http://www.example.com</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.7</maven.compiler.source>

<maven.compiler.target>1.7</maven.compiler.target>

<spring.version>5.0.2.RELEASE</spring.version>

<slf4j.version>1.6.6</slf4j.version>

<log4j.version>1.2.12</log4j.version>

<MySQL.version>5.1.6</mysql.version>

<mybatis.version>3.4.5</mybatis.version>

</properties>

<dependencies>

<!-- spring -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.6.8</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>com.microsoft.sqlserver</groupId>

<artifactId>sqljdbc4</artifactId>

<version>4.0</version>

</dependency>

<dependency>

<groupId>JAVAx.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<!-- log start -->

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>${log4j.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${slf4j.version}</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>${slf4j.version}</version>

</dependency>

<!-- log end -->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>${mybatis.version}</version>

</dependency>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>1.3.0</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>c3p0</groupId>

<artifactId>c3p0</artifactId>

<version>0.9.1.2</version>

<type>jar</type>

<scope>compile</scope>

</dependency>

</dependencies>

<build>

<finalName>SSM</finalName>

<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

<plugins>

<plugin>

<artifactId>maven-clean-plugin</artifactId>

<version>3.1.0</version>

</plugin>

<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->

<plugin>

<artifactId>maven-resources-plugin</artifactId>

<version>3.0.2</version>

</plugin>

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.0</version>

</plugin>

<plugin>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.1</version>

</plugin>

<plugin>

<artifactId>maven-war-plugin</artifactId>

<version>3.2.2</version>

</plugin>

<plugin>

<artifactId>maven-install-plugin</artifactId>

<version>2.5.2</version>

</plugin>

<plugin>

<artifactId>maven-deploy-plugin</artifactId>

<version>2.8.2</version>

</plugin>

</plugins>

</pluginManagement>

</build>

</project>

3、部署ssm_web的項目,只要把ssm_web項目加入到Tomcat服務器中即可

4、編寫實體類

package com.mingqi.domain;

import java.io.Serializable;

public class Account implements Serializable {

public void setId(Integer id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public void setMoney(Double money) {

this.money = money;

}

public Integer getId() {

return id;

}

public String getName() {

return name;

}

public Double getMoney() {

return money;

}

private Integer id;

private String name;

private Double money;

@Override

public String toString() {

return "Account{" +

"id=" + id +

", name='" + name + ''' +

", money=" + money +

'}';

}

}

5、編寫dao接口

package com.mingqi.dao;

import com.mingqi.domain.Account;

import org.apache.ibatis.annotations.Insert;

import org.apache.ibatis.annotations.Select;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public interface IAccountDao {

@Insert(value="insert into account (name,money) values (#{name},#{money})")

public void saveAccount(Account account);

@Select("select * from account")

public List<Account> findAll();

}

6、編寫service接口和實現類

package com.mingqi.service;

import com.mingqi.domain.Account;

import java.util.List;

public interface IAccountService {

public void saveAccount(Account account);

public List<Account> findAll();

}

package com.mingqi.service.impl;

import com.mingqi.dao.IAccountDao;

import com.mingqi.domain.Account;

import com.mingqi.service.IAccountService;

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

import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountService")

public class AccountImpl implements IAccountService {

@Autowired

private IAccountDao accountDao;

@Override

public void saveAccount(Account account) {

accountDao.saveAccount(account);

}

@Override

public List<Account> findAll() {

System.out.println("業務層:查詢所有賬戶...");

List<Account> accounts=accountDao.findAll();

return accounts;

//return null;

}

}

二、Spring框架代碼的編寫

1. 搭建和測試Spring的開發環境

在webapp項目中創建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:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 開啟注解掃描,要掃描的是service和dao層的注解,要忽略web層注解,因為web層讓SpringMVC框架

去管理 -->

<context:component-scan base-package="com.mingqi">

<!-- 配置要忽略的注解 -->

<context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

<!-- 配置C3P0的連接池對象 -->

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/>

<property name="username" value="sa"/>

<property name="password" value="123sa"/>

</bean>

<!-- 配置SqlSession的工廠 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 配置掃描dao的包 -->

<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.mingqi.dao"/>

</bean>

<!--配置Spring框架聲明式事務管理-->

<!--配置事務管理器-->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!--配置事務通知-->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

<tx:method name="find*" read-only="true"/>

<tx:method name="*" isolation="DEFAULT"/>

</tx:attributes>

</tx:advice>

<aop:config>

<aop:advisor advice-ref="txAdvice" pointcut="execution(public * com.mingqi.service..ServiceImpl.*(..))"/>

</aop:config>

</beans>

三、Spring整合SpringMVC框架

1、搭建和測試SpringMVC的開發環境

1. 在web.xml中配置DispatcherServlet前端控制器

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

<display-name>Archetype Created Web Application</display-name>

<!--配置Spring的監聽器,默認只加載WEB-INF目錄下的applicationContext.xml配置文件-->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--設置配置文件的路徑-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<context-param>

<param-name/>

<param-value/>

</context-param>

<!--配置前端控制器-->

<servlet>

<servlet-name>dispatcherServlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!--加載springmvc.xml配置文件-->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<!--啟動服務器,創建該servlet-->

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<!--解決中文亂碼的過濾器-->

<filter>

<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

2、測試SpringMVC的框架搭建是否成功

1. 編寫index.jsp和list.jsp編寫,超鏈接

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<body>

<h2>查詢所有!</h2>

<a href="account/findAll">查詢所有</a>

<a href="account/findAll">測試查詢</a>

<h3>測試包</h3>

<form action="account/save" method="post">

姓名:<input type="text" name="name" /><br/>

金額:<input type="text" name="money" /><br/>

<input type="submit" value="保存"/><br/>

</form>

</body>

</html>

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>

<title>Title</title>

</head>

<body>

<h3>查詢所有的帳戶</h3>

<c:forEach items="${list}" var="account">

${account.name}

</c:forEach>

</body>

</html>

2、創建AccountController類,編寫方法,進行測試

package com.mingqi.controller;

import com.mingqi.domain.Account;

import com.mingqi.service.IAccountService;

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 javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.util.List;

@Controller

@RequestMapping("/account")

public class AccountController {

@Autowired

private IAccountService accountService;

/**

* 查詢所有數據

* @return

*/

@RequestMapping("/findAll")

public String findAll(Model model)

{

System.out.println("表現層查詢所有");

accountService.findAll();

List<Account> list = accountService.findAll();

model.addAttribute("list",list);

return "list";

}

@RequestMapping("/save")

public void save(Account account, HttpServletRequest request, HttpServletResponse response) throws IOException

{

accountService.saveAccount(account);

response.sendRedirect(request.getContextPath()+"/account/findAll");

return;

}

}

配置tomcat,啟動項目,進行測試

推薦閱讀:

java開發之Java 虛擬機棧

Java開發中SSM框架整合開發之 Spring IoC

Java開發框架之Spring JDBC

分享到:
標簽:框架 開發
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定