Frame using SSM (on)

Frame using SSM (on)

Preparation for Use

1. Create a new entity class

Here Insert Picture Description
Here Insert Picture Description
In addition to constructing a constructor with no arguments, I created a
Here Insert Picture Description
total score can be calculated, rankings do not own assignment, getter and setter methods being no special requirements (in fact, there are some details but had little effect)

2. Create an entry in the database based on the entity class

Here Insert Picture Description
Remember the name of the database you have created (below will be used), the table name student, studnet table attribute names as much as possible consistent with the name of the entity class, inconsistent, then later can establish a mapping relationship between themselves, but the type must be consistent.
After adding a data to get that done inside.
Here Insert Picture Description

3. the introduction of a respective jar package

A total of 27, which is incorporated on the jar package is not explained, the jar package placed in the WEB-INF lib directory on the line.
Their role description

4. Write the configuration file

Resources to create a new resource in the project directory, the directory into profile

  1. application-jdbc.xml this is spring configuration information
  2. This is a database of information database.properties
  3. This is the information log4j.properties log output, or else you may not get to use the log output, but recommended
  4. mybatis-config.xml this configuration information in Mybats
  5. springmvc-servlet.xml this is the configuration information springmvc

Start with a simple walk from:

4.1 database.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/studentscoremanager?useUnicode=true&characterEncoding=utf-8
user=root
password=123456

studentscoremanager this is my database name (the name of the establishment of a database in mysql)
my username is root, mysql password is 123456, these will need to change their own.

4.2. log4j.properties

This is also very fixed, any, would not have changed (log normal output)

log4j.rootLogger=DEBUG,CONSOLE,file

log4j.logger.org.mybatis=DEBUG
log4j.logger.com.ibatis=debug 
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
log4j.logger.java.sql.Connection=debug 
log4j.logger.java.sql.Statement=debug 
log4j.logger.java.sql.PreparedStatement=debug 
log4j.logger.java.sql.ResultSet=debug 
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n

log4j.logger.com.opensymphony.xwork2=error  
4.3 Mybatis-config.xml
<?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.studentManager.pojo"/>
	 </typeAliases>
</configuration>

That is, under the category cn.studentManager.pojo path (my entity class Student here) I can directly use the class name, you do not have to add a path (in Mapper file sql statement)

4.4 aplication-jdbc.xml

The chicken thief and more

<?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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- 读取数据库配置文件-->
	<context:property-placeholder location="classpath:database.properties"/>
	<!-- JNDI获取数据源(使用dbcp连接池) --> 
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
		<property name="driverClassName" value="${driver}" />  
		<property name="url" value="${url}" />  
		<property name="username" value="${user}" />  
		<property name="password" value="${password}" />
	</bean>
	<!-- 配置SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 引用数据源组件 -->
	  	<property name="dataSource" ref="dataSource" />
		  <!-- 引用MyBatis配置文件中的配置 -->
		  <property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>
	
	<!-- 这下面的内容需要等到写完 dao 和 service 之后再加入-->
	<!-- 自动查找Mapper,自动注册对应的MapperFactoryBean对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.studentManager.dao"/>
	</bean>
	
	<!-- 配置扫描注解定义 -->
	<!-- 让spring扫描指定包下的所有类,如果扫描到 @Component、@Service、、@Repository等这些注解的类,则自动把这些类标注为Bean组件 -->
	<!-- <context:component-scan base-package="cn.student.dao"/> -->
	<context:component-scan base-package="cn.studentManager.service"/>
</beans>
5. Start using

springmvc-servlet.xml wait until later when we again explained
Here Insert Picture Description
in StudentMapper.java I defined an interface that is used to find information of all students
Here Insert Picture Description
in StudentMapper.xml the interface, note xml file name and the same interface name must
Here Insert Picture Description
in StudentService.java I defined an interface
Here Insert Picture Description
in StudentServiceImp.java I comply with this interface, query methods StudentMapper in and then call in here, and query the data returned.
Here Insert Picture Description
[Note] annotated @Service in a class above, will be registered as Spring Bean Components

6. Test

In StudentTest write test code and run
Here Insert Picture Description
the results:
Here Insert Picture Description
Here Insert Picture Description
such a result to prove it, I will be involved in the follow-up section of Spring mvc. If the above error code or content, please remind me, I will continue revised, thank you!

Published 27 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43932553/article/details/104930817