SSM Knowledge Integration

Environment to build

Report: requirements engineering process

Here Insert Picture Description
1, build a development environment
jdk8, tomcat7 + the Eclipse | MyEclipse | IDEA
2, the project environment
to create a project, add a jar package dependencies (spring core package, the package springMVC package, mybatis package, driver package database connection pool, apache the commons series, log4 j series bold style, grammar package jstl): there are two ways
2.1 maven add a dependency (build warehouses, introducing rely address the pom.xml configuration)
2.2 manually add local jar package (for novice)

ssm integrate basic configuration

ssm develop a separate development time: Use a separate spring, you need applicationContext.xml; springMVC use alone, need springMVC.xml
use mybatis alone, need mybatis.xml file
so: integration, we still need three configuration files
to create a profile : applicationContext.xml, springMVC.xml, mybatis.xml profile, head restraint and namespace configuration files, etc.
If this is the first time configuration, the official documentation of each profile are set up with a good profile, you can download
ssm framework in spring role is: to play for other frameworks as well as classes and bond management: management, transaction management and other objects as well as a number of other development model
main ideas: notes, reflection, aspect-oriented programming. . .
springMVC: controlled based on the servlet front, instead of the servlet end of interaction, the MVC pattern
mybatis: main database operation, dynamic sql statement, and other secondary cache
configuration in web.xml applicationContext.xml files and settings springMVC.xml load

Spring loaded container:

CLASSPATH the contextConfigLocation: the applicationContext.xml org.springframework.web.context.ContextLoaderListener springMVC load: the DispatcherServlet org.springframework.web.servlet.DispatcherServlet the contextConfigLocation CLASSPATH: springMVC.xml the DispatcherServlet * .do encoding format filter: filtering is primarily Chinese characterFilter org.springframework.web.filter.CharacterEncodingFilter characterFilter / * applicationContext.xml about the database configuration file:
<bean  id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <!-- 5.+找对应的驱动包,
         8+ 可以使用专门的驱动包,也可以使用5系列的驱动包,比如8对应5系列5.1.45包 
         如果用8系列的驱动包,驱动包名 com.mysql.cj.jdbc.Driver-->
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
       <!--?后面的是附属的约束条件,由于环境等问题可能需要;
           连接协议:数据库类型://主机地址:端口号/数据名字?约束条件  -->
       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8"></property>
       <property name="user"  value="root"></property>
       <property name="password" value="1234"></property>
</bean>

<!--sqlsessionFactory,通过工厂模式管理  -->
<bean  id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="configLocation" value="classpath:mybatis.xml"></property>
   <property name="dataSource"    ref="dataSource"></property>
</bean>

<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
	<tx:attributes>
		<tx:method name="save*" propagation="REQUIRED"/>
		<tx:method name="get*" propagation="SUPPORTS"/>
	</tx:attributes>
</tx:advice>

access

Access Interface

Jsp the jsp files in the file under the following webContent WEB-INF folder, other static resource files in the webContent on it
under webContent folder, in addition to the given two folders, the rest are open to the general static resources can be placed directly here
jsp file we do not want people to directly access, then placed under WEB-INF

Writing code:
Create a method in the controller, returns the name of the page

Mentoring in the configuration parser springMVC

Access on the browser

Access database data

Function: Display a Directory and secondary directories on the home page
1, controller request path and create matching method: When the interface is loaded, the data have to be loaded on, so we still use the same interface and an access request ( a need to re-write controller and the corresponding method)
2, POJO classes: database table and use the association can, if cascade, add association attributes (foreign key may be used directly)
3, create an interface mapper

public interface CategoryMapper {

/*查询所有一级目录和二级目录*/
public List<Category> selectAll();

}
4. Create a mapper xml file. Write sql statement

<!-- 查询所有的目录,返回查询结果集,结果集存在级联的问题
          一对一,一对多,多对多
          一对一:
          一对多:collection
  -->
select category. *, categorysecond. * from category, categorysecond where category.cid = categorysecond.cid 5, configuration mapper in applicationContext.xml document scanner (for the first time to write it, do not have to follow the configuration)
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage"  value="com.qf.edu.mapper"></property>
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>

6, service creation and writing business logic layer

public class CategoryService {

@Autowired
public CategoryMapper categoryMapper;

/*查询所有的目录*/
public  List<Category>   selectAllCategory(){
	//调用mapper中方法,获取数据库操作结果
	List<Category> list=categoryMapper.selectAll();
	if(list!=null&&list.size()>0){
		return list;
	}else{
		return null;
	}
		
}

}
7, then a service configuration applicationContext the bean
. 8, the controller back to the method, the result is returned to the front end
can be returned using the Model may be used to return the dialogue session, may be packaged into json string, returns a response

9, the front end receiver
may receive a syntax jstl may also be received by the session, may also be received using ajax

Guess you like

Origin blog.csdn.net/weixin_43812609/article/details/95254495