1.數據庫css
2.總體空架構(jar包)前端
3.工程依賴 24節點 build節點java
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--spring對應的版本號--> <spring.version>4.3.10.RELEASE</spring.version> </properties> <!--引入須要的spring 核心jar--> <dependencies> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!--SpringJDBC須要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--spring整合aspect須要的jar--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!--引入spring須要的事務jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!--引入springmvc須要的jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--引入mysql的驅動包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--配置jdbc須要的數據源--> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <!--aspectj須要的jar--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!--引入須要的jstl jar--> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.javassist/javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.8</version> </dependency> </dependencies>
<build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build>
4.4個配置文件mysql
1.配置web.xml節點web
1.context-param
2.編碼過濾器
3. lister
4. Springmvc前端控制器
5.靜態資源放行,交給Tomcat默認的servelt解決spring
<!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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--解決編碼的過濾器--> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Springmvc配置,前端控制器,就是一個Servlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> </web-app>
2.配置mybatis-config.xmlsql
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--別名--> <typeAliases> <package name="cn.happy.bean"></package> </typeAliases> </configuration>
3.配置jdbc.properties數據庫
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///smbms jdbc.userName=root jdbc.password=1234
4.配置applicationContext.xmlexpress
<?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:mvc="http://www.springframework.org/schema/mvc" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--包掃描器--> <context:component-scan base-package="cn.happy"/> <!--mvc註解驅動 Json Object--> <mvc:annotation-driven/> <!--1.數據源 dbcp Spring自帶的 c3p0 Druid--> <!--使用dbcp數據源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.jdbcUrl}"/> <property name="username" value="${jdbc.userName}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--2.識別jdbc.properties文件一個節點--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--3.SessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--4.掃描Mapper--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.happy.dao"/> </bean> <!--配置jsp顯示ViewResolver--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <!--事務管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--9.事務--> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
5.分層apache
1.Bean
import java.io.Serializable; import java.sql.Timestamp; import java.util.Date; //用戶 public class User implements Serializable { private Integer id; private String userCode; private String userName; private String userPassword; private Integer gender; private Date birthday; private String phone; private String address; private Integer userType; private Integer createdBy; private Timestamp creationDate; private Integer modifyBy; private Timestamp modifyDate; public User() { } public User(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userType, Integer createdBy, Timestamp creationDate, Integer modifyBy, Timestamp modifyDate) { this.id = id; this.userCode = userCode; this.userName = userName; this.userPassword = userPassword; this.gender = gender; this.birthday = birthday; this.phone = phone; this.address = address; this.userType = userType; this.createdBy = createdBy; this.creationDate = creationDate; this.modifyBy = modifyBy; this.modifyDate = modifyDate; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserCode() { return userCode; } public void setUserCode(String userCode) { this.userCode = userCode; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getUserType() { return userType; } public void setUserType(Integer userType) { this.userType = userType; } public Integer getCreatedBy() { return createdBy; } public void setCreatedBy(Integer createdBy) { this.createdBy = createdBy; } public Timestamp getCreationDate() { return creationDate; } public void setCreationDate(Timestamp creationDate) { this.creationDate = creationDate; } public Integer getModifyBy() { return modifyBy; } public void setModifyBy(Integer modifyBy) { this.modifyBy = modifyBy; } public Timestamp getModifyDate() { return modifyDate; } public void setModifyDate(Timestamp modifyDate) { this.modifyDate = modifyDate; } @Override public String toString() { return "User{" + "id=" + id + ", userCode='" + userCode + '\'' + ", userName='" + userName + '\'' + ", userPassword='" + userPassword + '\'' + ", gender=" + gender + ", birthday=" + birthday + ", phone='" + phone + '\'' + ", address='" + address + '\'' + ", userType=" + userType + ", createdBy=" + createdBy + ", creationDate=" + creationDate + ", modifyBy=" + modifyBy + ", modifyDate=" + modifyDate + '}'; } }
3.Dao
1.用戶Dao接口層
import cn.happy.bean.User; import cn.happy.util.PageUtil; import java.util.List; import java.util.Map; /** * 用戶接口層 */ public interface IUserDao { //1.登陸方法 public User isLog(User user); //檢索全部用戶的集合 public List<User> findAllUsers(); //獲取總記錄數 public int getTotalCount(); //獲取單頁數據 public List<User> getOnePageData(Map<String, Object> map); PageUtil getOnePageData(int pageIndex, int pageSize); }
用戶Dao實現層(用Mybatis實現)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.happy.dao.IUserDao"> <!--登陸--> <select id="isLog" parameterType="User" resultType="User"> select userCode,userName FROM smbms_user WHERE userCode=#{userCode} AND userPassword=#{userPassword} </select> <!--2.檢索全部用戶集合--> <select id="findAllUsers" resultType="User"> SELECT * from smbms_user </select> <!--獲取總記錄數--> <select id="getTotalCount" resultType="int"> SELECT count(1) from smbms_user </select> <!--獲取單頁數據--> <select id="getOnePageData" resultType="User"> SELECT * from smbms_user limit #{pageIndex},#{pageSize} </select> </mapper>
2.Service
import cn.happy.bean.User; import cn.happy.util.PageUtil; import java.util.List; public interface IUserService { //1.登陸方法 public User isLog(User user); //檢索全部用戶的集合 public List<User> findAllUsers(); //獲取總記錄數 public int getTotalCount(); //獲取單頁數據 public PageUtil getOnePageData(int pageIndex, int pageSize); }
UserServiceImpl
import cn.happy.bean.User; import cn.happy.dao.IUserDao; import cn.happy.service.IUserService; import cn.happy.util.PageUtil; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by Administrator on 2017/10/30. */ @Service("userService") public class IUserServiceImpl implements IUserService { //dao層對象 @Resource(name = "IUserDao")//將Spring容器懸浮的哪一個DAO對象賦值給成員變量 private IUserDao userDao; @Override public User isLog(User user) { System.out.println("service"); return userDao.isLog(user); } @Override public List<User> findAllUsers() { return userDao.findAllUsers(); } @Override public int getTotalCount() { return 0; } @Override public PageUtil getOnePageData(int pageIndex, int pageSize) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pageIndex", pageIndex * pageSize); map.put("pageSize", pageSize); PageUtil page = new PageUtil();//實例化一個PageUtil對象 page.setPageSize(pageSize);//給PageUtil屬性賦值 page.setPageIndex(pageIndex);//給PageUtil的pageIndex賦值 int totalCount = userDao.getTotalCount(); page.setTotalRecords(totalCount); int totalPage = page.getTotalRecords() % page.getPageSize() == 0 ? page.getTotalRecords() / page.getPageSize() : page.getTotalRecords() / page.getPageSize() + 1; page.setTotalPages(totalPage); List<User> list = userDao.getOnePageData(map); page.setList(list); return page; } }