ssm整合
構圖
目錄結構圖
Controller.CategoryController
package com.how2java.controller;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.Contended;
import java.util.List;
@Controller
@RequestMapping("")
public class CategoryController {
@Autowired
CategoryService categoryService;
@RequestMapping("listCategory")
public ModelAndView listCategory(){
ModelAndView mv=new ModelAndView();
List<Category> cs=categoryService.list();
mv.addObject("cs",cs);
mv.setViewName("listCategory");
return mv;
}
}
mapper.Category.xml
<?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="com.how2java.mapper.CategoryMapper">
<insert id="add" parameterType="Category" >
insert into category_ ( name ) values (#{name})
</insert>
<delete id="delete" parameterType="Category" >
delete from category_ where id= #{id}
</delete>
<select id="get" parameterType="_int" resultType="Category">
select * from category_ where id= #{id}
</select>
<update id="update" parameterType="Category" >
update category_ set name=#{name} where id=#{id}
</update>
<select id="list" resultType="Category">
select * from category_
</select>
</mapper>
mapper.CategoryMapper
package com.how2java.mapper;
import com.how2java.pojo.Category;
import java.util.List;
public interface CategoryMapper {
public int add(Category Category);
public void delete(int id);
public Category get(int id);
public int update(Category category);
public List<Category> list();
public int count();
}
pojo.Category
package com.how2java.pojo;
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Service.CategoryService
package com.how2java.service;
import com.how2java.pojo.Category;
import java.util.List;
public interface CategoryService {
List<Category> list();
}
Service.CategoryServcieImpl
package com.how2java.service.impl;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import com.how2java.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;
@Override
public List<Category> list() {
return categoryMapper.list();
}
}
test.MybatisTest
package com.how2java.test;
import com.how2java.mapper.CategoryMapper;
import com.how2java.pojo.Category;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MybatisTest {
@Autowired
private CategoryMapper categoryMapper;
@Test
public void test(){
Category category=new Category();
category.setName("new Category");
categoryMapper.add(category);
}
@Test
public void list(){
List li= categoryMapper.list();
System.out.println(li);
}
}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.how2java.service"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.how2java.pojo"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/how2java/mapper/Category.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.how2java.mapper"></property>
</bean>
</beans>```
log4j.properties
```java
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.how2java=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n```
springMVC.xml
```java
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.how2java.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
相關文章
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC
- SSM框架整合SSM框架
- 整合SSM框架SSM框架
- SSM整合步驟SSM
- SSM整合開發SSM
- SpringMVC-整合SSMSpringMVCSSM
- SSM框架的整合SSM框架
- 八、SpringMVC——ssm整合SpringMVCSSM
- ssm整合(maven版本)SSMMaven
- ssm整合詳解SSM
- SSM整合(Spring、SpringMVC、Mybatis)SSMSpringMVCMyBatis
- ssm框架整合筆記SSM框架筆記
- SSM——Spring整合SpringMVC,MyBatisSSMSpringMVCMyBatis
- SSM(SpringMVC + Spring + Mybatis)整合SSMSpringMVCMyBatis
- SSM框架整合開發SSM框架
- SSM框架整合流程SSM框架
- SSM整合之CRUD環境搭建整合SSM
- Mybatis20_ssm整合10MyBatisSSM
- ssm高階整合 尚矽谷SSM
- SSM框架整合(配置檔案)SSM框架
- SSM專案整合——後端SSM後端
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- Maven整合SSM和Redis,親測MavenSSMRedis
- 【Java】SSM框架整合 附原始碼JavaSSM框架原始碼
- 使用xml檔案配置SSM整合XMLSSM
- mybatis與spring整合ssm01MyBatisSpringSSM
- SpringMVC(3)- 校驗框架、SSM整合SpringMVC框架SSM
- SSM三大框架整合詳細教程SSM框架
- SSM框架整合——書籍管理系統SSM框架
- ssm框架整合redis(demo)---通過cache方式SSM框架Redis
- SSM 框架快速整合例項--學生查詢SSM框架
- SSM(Spring-MyBatis-SpringMVC)框架整合【完整版】SSMMyBatisSpringMVC框架
- 【死磕Sharding-jdbc】—基於 SSM 整合shardingJDBCSSM
- [Android]後端之路--整合SSM(Spring+SpringMVC+MyBatis)框架(2)Android後端SSMSpringMVCMyBatis框架
- SSM三大框架整合詳細教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- SSM整合之使用配置類替換xml配置檔案(2)SSMXML
- SpringMVC mybatis SSM 整合程式碼生成器 java redis shiro ehcacheSpringMVCMyBatisSSMJavaRedis
- 【SSM框架整合】專案xml檔案、properties等檔案的配置SSM框架XML