SSM整合笔记
1,创建maven项目
2,在pom.xml里面导入相应的jar的依赖
<!-- 导入springmvc -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/org.springframework/spring-webmvc --></span>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/org.springframework/spring-jdbc --></span>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
<!-- mybatis -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/org.mybatis/mybatis --></span>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.3</version>
</dependency>
<!-- 连接池commons-dbcp -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/commons-dbcp/commons-dbcp --></span>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 数据库驱动 mysql -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/mysql/mysql-connector-java --></span>
<dependency>
<groupId><a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a></groupId>
<artifactId><a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<!-- mybatis-spring -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/org.mybatis/mybatis-spring --></span>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- jstl -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/jstl/jstl --></span>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- js -->
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --></span>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.4</version>
</dependency>
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --></span>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --></span>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https:<span style="color: #008000">//</span><span style="color: #008000">mvnrepository.com/artifact/com.google.code.gson/gson --></span>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<!-- 引入PageHelper分页插件 -->
<dependency>
<groupId>com.github.<a href="/tag/pageHelper/" target="_blank" class="keywords">pageHelper</a></groupId>
<artifactId><a href="/tag/pageHelper/" target="_blank" class="keywords">pageHelper</a></artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SSM1</finalName>
</build>
3,构建基本目录
后台页面目录
文件
首页
文件
4,文件配置
数据库
数据库连接配置文件 db.properties(new一个file)
内容
MysqL、sql server,3者的写法均不同变量名可以随意取,但是要注意,不能和系统内置的变量名起冲突等号右边的内容,不要加双引号或单引号
MysqL的配置
MysqL.jdbc.Driver;//数据库驱动
MysqL://localhost:3306/blog?characterEncoding=utf-8 //数据库地址(一般写ip地址)
数据库用户名
数据库密码
MysqL.jdbc.Driver jdbc.url =jdbc:MysqL://localhost:3306/blog?characterEncoding=utf-8 jdbc.username = root jdbc.password =root
配置文件 spring-mybatis.xml
----------------------------------------------------------------------------------------
详细解释
---------------------------------------------------------------------------------------
<!-- 引入数据库配置文件 -->
<!-- <util:properties id="config"
></util:properties> -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据连接参数及连接池 -->
<bean id="dataSource"
<span style="color: #0000ff">class</span>="org.apache.commons.dbcp.BasicDataSource"<span style="color: #000000">
destroy</span>-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="sqlSessionfactorybean" <span style="color: #0000ff">class
="org.mybatis.spring.sqlSessionfactorybean"><property name="dataSource" ref="dataSource"/>
<!-- 映射文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 分页插件pageHelper -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<bean <span style="color: #0000ff">class
="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.junge.dao*"/>
<!-- <property name="<a href="/tag/sql/" target="_blank" class="keywords">sql</a>Session<a href="/tag/factorybean/" target="_blank" class="keywords">factorybean</a>Name" value="<a href="/tag/sql/" target="_blank" class="keywords">sql</a>Session<a href="/tag/factorybean/" target="_blank" class="keywords">factorybean</a>"></property> -->
配置文件 spring-mvc.xml
----------------------------------------------------------------
详细解释
----------------------------------------------------------------
--------------------
源代码
--------------------
<!-- 启用注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 处理静态资源 -->
<mvc:<span style="color: #0000ff">default</span>-servlet-handler />
<!-- 开启组件扫描 -->
<context:component-<span style="color: #000000">scan
base</span>-<span style="color: #0000ff">package</span>="com.junge.*" />
<!-- 配置视图解析器 -->
<<span style="color: #000000">bean
</span><span style="color: #0000ff">class</span>="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 拦截器 -->
<mvc:interceptors>
<!--<span style="color: #000000"> 多个<a href="/tag/lanjie/" target="_blank" class="keywords">拦截</a>器顺序执行
<a href="/tag/denglu/" target="_blank" class="keywords">登录</a>认证<a href="/tag/lanjie/" target="_blank" class="keywords">拦截</a>器 </span>-->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean <span style="color: #0000ff">class</span>="com.junge.interceptor.Interceptor" />
</mvc:interceptor>
</mvc:interceptors>
<!--全局异常处理 只要你实现了HandelExceptionResolver接口 , 这个类就是一个全局异常处理类 -->
<bean <span style="color: #0000ff">class</span>="com.junge.exception.JExceptionResolver"></bean>
<!-- 文件上传 -->
<bean id="multipartResolver" <span style="color: #0000ff">class</span>="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置默认编码 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件总的大小最大为5M -->
<property name="maxUploadSize" value="5242440"></property>
</bean>
----------------------------------------------------------------
详细解释
----------------------------------------------------------------
--------------------------------------------------------------------------------------
源代码
--------------------------------------------------------------------------------------
<!-- spring mvc 请求响应 -->
<!-- The front controller of this Spring Web application,responsible for
handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value><!-- 这么写会自动找到之前写的 spring-mybaits文件和spring-mvc文件 -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<!-- 表示后台控制层接受。action和json结尾的请求 本项目中利用action请求转发页面 json请求转发json数据 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置静态资源文件路径 -->
<servlet-mapping>
<servlet-name>default</servlet-name><!-- 表示static目录下的文件都是静态文件 -->
<url-pattern>/static/*</url-pattern><!-- 不配置的话,框架会认为这些文件的路径都是请求,导致无法访问 -->
</servlet-mapping>
<!-- 配置错误页面 -->
<error-page>
<error-code><a href="/tag/403/" target="_blank" class="keywords">403</a></error-code>
<location>/WEB-INF/page/portal/<a href="/tag/403/" target="_blank" class="keywords">403</a>.jsp</location>
</error-page>
<error-page>
<error-code><a href="/tag/404/" target="_blank" class="keywords">404</a></error-code>
<location>/WEB-INF/page/portal/<a href="/tag/404/" target="_blank" class="keywords">404</a>.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/page/portal/500.jsp</location>
</error-page>
分页的配置文件(pageHelper)
数据库数据
数据库里面user是关键字)
<span style="color: #0000ff">private <span style="color: #0000ff">int<span style="color: #000000"> user_id;
<span style="color: #0000ff">private<span style="color: #000000"> String user_name;
<span style="color: #0000ff">private<span style="color: #000000"> String user_password;
<span style="color: #0000ff">public <span style="color: #0000ff">int<span style="color: #000000"> getUser_id() {
<span style="color: #0000ff">return<span style="color: #000000"> user_id;
}
<span style="color: #0000ff">public <span style="color: #0000ff">void setUser_id(<span style="color: #0000ff">int<span style="color: #000000"> user_id) {
<span style="color: #0000ff">this.user_id =<span style="color: #000000"> user_id;
}
<span style="color: #0000ff">public<span style="color: #000000"> String getUser_name() {
<span style="color: #0000ff">return<span style="color: #000000"> user_name;
}
<span style="color: #0000ff">public <span style="color: #0000ff">void<span style="color: #000000"> setUser_name(String user_name) {
<span style="color: #0000ff">this.user_name =<span style="color: #000000"> user_name;
}
<span style="color: #0000ff">public<span style="color: #000000"> String getUser_password() {
<span style="color: #0000ff">return<span style="color: #000000"> user_password;
}
<span style="color: #0000ff">public <span style="color: #0000ff">void<span style="color: #000000"> setUser_password(String user_password) {
<span style="color: #0000ff">this.user_password =<span style="color: #000000"> user_password;
}
}
登录的方法
<span style="color: #0000ff">import<span style="color: #000000"> com.ssm.view.User;
<span style="color: #0000ff">public <span style="color: #0000ff">interface<span style="color: #000000"> UserDao {
<span style="color: #008000">/**<span style="color: #008000">
- 登录操作
- <span style="color: #808080">@param<span style="color: #008000"> user_name 用户名
- <span style="color: #808080">@param<span style="color: #008000"> user_password 密码
- <span style="color: #808080">@return
<span style="color: #008000">*/
<span style="color: #0000ff">public User login(@Param("user_name")String user_name,@Param("user_password"<span style="color: #000000">)String user_password);
}
文件夹 用来放mybatis dao的配置文件
文件 名称为UserDaomapper.xml
<mapper namespace="com.ssm.dao.UserDao">
<resultMap id="UserMap" type="com.ssm.view.User"><!-- 对应的实体类的路径 -->
<result column="user_id" property="user_id" />
<result column="user_name" property="user_name" />
<result column="user_password" property="user_password" />
</resultMap>
<select id="login" resultMap="UserMap"><span style="color: #000000">
SELECT
</span>*<span style="color: #000000">
FROM
user
WHERE
user_name</span>=#{user_name} and user_password=<span style="color: #000000">#{user_password}
</span></select>
登录的方法
<span style="color: #0000ff">import<span style="color: #000000"> org.springframework.stereotype.Service;
<span style="color: #0000ff">import<span style="color: #000000"> com.ssm.dao.UserDao;
<span style="color: #0000ff">import<span style="color: #000000"> com.ssm.view.User;
@Service("userService"<span style="color: #000000">)
<span style="color: #0000ff">public <span style="color: #0000ff">class<span style="color: #000000"> UserService {
@Autowired
<span style="color: #0000ff">private<span style="color: #000000"> UserDao userDao;
<span style="color: #008000">/**<span style="color: #008000">
- 登录操作
- <span style="color: #808080">@param<span style="color: #008000"> user_name
- <span style="color: #808080">@param<span style="color: #008000"> user_password
- <span style="color: #808080">@return
<span style="color: #008000">*/
<span style="color: #0000ff">public<span style="color: #000000"> User login(String user_name,String user_password) {
<span style="color: #0000ff">return<span style="color: #000000"> userDao.login(user_name,user_password);
}
}
页面(登录)
页面