├── .settings ├── .jsdtscope ├── org.eclipse.core.resources.prefs ├── org.eclipse.jdt.core.prefs ├── org.eclipse.m2e.core.prefs ├── org.eclipse.wst.common.component ├── org.eclipse.wst.common.project.facet.core.xml ├── org.eclipse.wst.jsdt.ui.superType.container └── org.eclipse.wst.jsdt.ui.superType.name ├── README.md ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── mybatis │ │ └── spring │ │ ├── controller │ │ ├── HelloController.java │ │ ├── IndexController.java │ │ ├── ProductController.java │ │ └── SimpleUrlController.java │ │ ├── mapper │ │ ├── CategoryMapper.java │ │ └── ProductMapper.java │ │ ├── model │ │ ├── Category.java │ │ ├── Product.java │ │ └── Products.java │ │ ├── service │ │ ├── CategoryService.java │ │ ├── ProductService.java │ │ └── impl │ │ │ ├── CategoryServiceImpl.java │ │ │ └── ProductServiceImpl.java │ │ └── utils │ │ └── UTF8StringHttpMessageConverter.java ├── resources │ ├── applicationContext.xml │ ├── jdbc.properties │ ├── log4j.properties │ ├── mybatis-config.xml │ ├── org │ │ └── mybatis │ │ │ └── spring │ │ │ └── mapper │ │ │ ├── CategoryMapper.xml │ │ │ └── ProductMapper.xml │ ├── spy.properties │ └── views.properties └── webapp │ └── WEB-INF │ ├── dispatche-servlet.xml │ ├── freemarker │ └── freemarker.ftl │ ├── velocity.properties │ ├── velocity │ └── velocity.vm │ ├── views │ ├── index.jsp │ ├── showuser.jsp │ └── welcome.jsp │ └── web.xml └── test └── java └── org └── mybatis └── spring └── service └── impl ├── CategoryServiceImplTest.java └── ProductServiceImplTest.java /.settings/.jsdtscope: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java/org/mybatis/spring/mapper/ProductMapper.java=UTF-8 3 | encoding//src/test/java/org/mybatis/spring/service/impl/ProductServiceImplTest.java=UTF-8 4 | encoding/README.md=UTF-8 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 4 | org.eclipse.jdt.core.compiler.compliance=1.5 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 8 | org.eclipse.jdt.core.compiler.source=1.5 9 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.component: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.common.project.facet.core.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.container: -------------------------------------------------------------------------------- 1 | org.eclipse.wst.jsdt.launching.baseBrowserLibrary -------------------------------------------------------------------------------- /.settings/org.eclipse.wst.jsdt.ui.superType.name: -------------------------------------------------------------------------------- 1 | Window -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring整合MyBatis 2 | 3 | #### 在控制台记录真实的sql语句 4 | 5 | #### 使用log4jdbc的配置 6 | 7 | ###### 1、添加log4jdbc的jar 8 | ```xml 9 | 10 | log4jdbc 11 | log4jdbc4 12 | 1.2 13 | 14 | 15 | log4jdbc 16 | log4jdbc4 17 | 1.2 18 | 19 | 20 | org.slf4j 21 | slf4j-api 22 | 1.6.6 23 | 24 | 25 | org.slf4j 26 | slf4j-log4j12 27 | 1.6.6 28 | 29 | 30 | org.slf4j 31 | slf4j-jdk14 32 | 1.5.8 33 | 34 | ``` 35 | ###### 2、参考[src/main/resources/log4j.properties](src/main/resources/log4j.properties)配置文件 36 | 37 | ###### 3、修改jdbc的driver和jdbc url(参考[src/main/resources/applicationContext.xml](src/main/resources/applicationContext.xml)) 38 | ```xml 39 | 40 | 41 | ``` 42 | #### 使用p6spy的配置 43 | 44 | ###### 1、添加p6spy的jar 45 | ```xml 46 | 47 | p6spy 48 | p6spy 49 | 1.3 50 | 51 | ``` 52 | ###### 2、参考[src/main/resources/spy.properties](src/main/resources/spy.properties) 53 | 54 | ###### 3、添加配置文件信息 55 | ```xml 56 | 57 | 58 | ``` 59 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | org.mybatis.spring 6 | mybatis-spring 7 | war 8 | 0.0.1-SNAPSHOT 9 | 10 | mybatis-spring 11 | http://maven.apache.org 12 | 13 | 14 | 15 | javax.servlet 16 | servlet-api 17 | 2.5 18 | provided 19 | 20 | 21 | javax.servlet.jsp 22 | jsp-api 23 | 2.1 24 | provided 25 | 26 | 27 | org.mybatis 28 | mybatis-spring 29 | 1.1.1 30 | 31 | 32 | mysql 33 | mysql-connector-java 34 | 5.1.14 35 | 36 | 37 | 38 | 39 | org.springframework 40 | spring-webmvc 41 | 3.1.1.RELEASE 42 | 43 | 44 | 45 | 46 | org.springframework 47 | spring-test 48 | 3.1.1.RELEASE 49 | 50 | 51 | 52 | 53 | 54 | org.codehaus.jackson 55 | jackson-mapper-asl 56 | 1.7.1 57 | 58 | 59 | 60 | junit 61 | junit 62 | 4.8.2 63 | 64 | 65 | javax.annotation 66 | jsr250-api 67 | 1.0 68 | 69 | 70 | log4j 71 | log4j 72 | 1.2.13 73 | 74 | 75 | p6spy 76 | p6spy 77 | 1.3 78 | 79 | 80 | log4jdbc 81 | log4jdbc4 82 | 1.2 83 | 84 | 85 | org.slf4j 86 | slf4j-api 87 | 1.6.6 88 | 89 | 90 | org.slf4j 91 | slf4j-log4j12 92 | 1.6.6 93 | 94 | 95 | org.slf4j 96 | slf4j-jdk14 97 | 1.5.8 98 | 99 | 100 | velocity 101 | velocity 102 | 1.4 103 | 104 | 105 | velocity 106 | velocity-dep 107 | 1.4 108 | 109 | 110 | org.freemarker 111 | freemarker 112 | 2.3.19 113 | 114 | 115 | javax.servlet 116 | jstl 117 | 1.2 118 | 119 | 120 | org.springframework 121 | spring-oxm 122 | 3.1.1.RELEASE 123 | 124 | 125 | javax.xml.bind 126 | jaxb-api 127 | 2.1 128 | 129 | 130 | 131 | 132 | mybatis-spring 133 | 134 | 135 | org.apache.maven.plugins 136 | maven-compiler-plugin 137 | 2.0.2 138 | 139 | 1.5 140 | 1.5 141 | 142 | 143 | 148 | 149 | org.apache.tomcat.maven 150 | tomcat6-maven-plugin 151 | 2.0 152 | 153 | 154 | org.apache.tomcat.maven 155 | tomcat7-maven-plugin 156 | 2.0 157 | 158 | 159 | 160 | 165 | 166 | org.mortbay.jetty 167 | maven-jetty-plugin 168 | 6.1.14 169 | 170 | 171 | 172 | 173 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/controller/HelloController.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/controller/HelloController.java -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/controller/IndexController.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.controller; 2 | 3 | import org.springframework.stereotype.Controller; 4 | import org.springframework.web.bind.annotation.RequestMapping; 5 | 6 | @Controller() 7 | @RequestMapping("/index") 8 | public class IndexController { 9 | 10 | 11 | public String index(){ 12 | return ""; 13 | } 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/controller/ProductController.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.controller; 2 | 3 | import java.util.List; 4 | 5 | import javax.annotation.Resource; 6 | 7 | import org.mybatis.spring.model.Product; 8 | import org.mybatis.spring.model.Products; 9 | import org.mybatis.spring.service.ProductService; 10 | import org.springframework.http.MediaType; 11 | import org.springframework.stereotype.Controller; 12 | import org.springframework.web.bind.annotation.PathVariable; 13 | import org.springframework.web.bind.annotation.RequestMapping; 14 | import org.springframework.web.bind.annotation.RequestMethod; 15 | import org.springframework.web.bind.annotation.ResponseBody; 16 | 17 | @Controller("productController") 18 | @RequestMapping("/product") 19 | public class ProductController { 20 | 21 | @Resource(name="productServiceImpl") 22 | private ProductService productService; 23 | 24 | 25 | @RequestMapping(value={"/find2json/{id}"},produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST}) 26 | public @ResponseBody Product findProductByIdToJson(@PathVariable("id")int id){ 27 | Product product=productService.findProductById(id); 28 | return product; 29 | } 30 | 31 | 32 | @RequestMapping(value={"/find/all/product/json"},produces=MediaType.APPLICATION_JSON_VALUE,method={RequestMethod.GET,RequestMethod.POST}) 33 | public @ResponseBody List findAllProductToJson(){ 34 | List products=productService.listAllProduct(); 35 | return products; 36 | } 37 | 38 | 39 | 40 | @RequestMapping(value={"/find/all/product/xml"},produces=MediaType.APPLICATION_XML_VALUE,method={RequestMethod.GET,RequestMethod.POST}) 41 | public @ResponseBody Products findAllProductToXML(){ 42 | Products products=new Products(); 43 | List lists=productService.listAllProduct(); 44 | products.setProducts(lists); 45 | return products; 46 | } 47 | 48 | 49 | 50 | 51 | @RequestMapping(value={"/find2xml/{id}"},produces=MediaType.APPLICATION_XML_VALUE,method={RequestMethod.GET,RequestMethod.POST}) 52 | public @ResponseBody Product findProductByIdToXML(@PathVariable("id")int id){ 53 | Product product=productService.findProductById(id); 54 | return product; 55 | } 56 | 57 | 58 | 59 | 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/controller/SimpleUrlController.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/controller/SimpleUrlController.java -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/mapper/CategoryMapper.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/mapper/CategoryMapper.java -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/mapper/ProductMapper.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.mapper; 2 | 3 | import java.util.List; 4 | 5 | import org.apache.ibatis.annotations.Param; 6 | import org.apache.ibatis.annotations.ResultMap; 7 | import org.apache.ibatis.annotations.Select; 8 | import org.apache.ibatis.annotations.Update; 9 | import org.mybatis.spring.model.Product; 10 | 11 | public interface ProductMapper { 12 | 13 | 14 | /** 15 | * 查询所有的商品 16 | * @return 17 | */ 18 | @Select("SELECT * FROM product INNER JOIN category ON product.categoryid=category.id") 19 | @ResultMap("productResultMap") 20 | List listAllProduct(); 21 | 22 | /** 23 | * 根据商品类别查询商品 24 | * @return 25 | */ 26 | @Select("select * from product where categoryid = #{categoryid}") 27 | @ResultMap("productResultMap") 28 | List listProductByCategoryid(@Param("categoryid")int categoryid); 29 | 30 | 31 | 32 | 33 | 34 | 35 | /** 36 | * 动态sql查询 37 | * @return 38 | */ 39 | List dynaQuerySql(Product product); 40 | 41 | 42 | 43 | 44 | 45 | /** 46 | * 级联查询 47 | * @return 48 | */ 49 | List listAllProductIncludeCategoryName(); 50 | 51 | 52 | /** 53 | * 新增 54 | * @param product 55 | */ 56 | void insertProduct(Product product); 57 | 58 | 59 | 60 | 61 | /** 62 | * 修改 63 | * @param product 64 | */ 65 | @Update("update product set name=#{product.name},price=#{product.price},description=#{product.description},categoryid=#{product.categoryid} where id = #{product.id} ") 66 | void updateProduct(@Param("product")Product product); 67 | 68 | 69 | 70 | 71 | /** 72 | * 根据id联合查询 73 | * @param product 74 | */ 75 | @Select("select * from product inner join category on product.categoryid=category.id where product.id = #{id}") 76 | @ResultMap("productResultMap") 77 | Product findProductById(@Param("id")int id); 78 | 79 | 80 | 81 | 82 | /** 83 | * 模糊查询 84 | * @param name 85 | * @return 86 | */ 87 | @Select("select * from product inner join category on product.categoryid=category.id where product.name like #{name} ") 88 | @ResultMap("productResultMap") 89 | List like(@Param("name")String name); 90 | 91 | 92 | 93 | 94 | 95 | /** 96 | * 分页查询 97 | * @param name 98 | * @return 99 | */ 100 | @Select("select * from product inner join category on product.categoryid=category.id order by product.id desc limit #{pageNumber},#{pageSize} ") 101 | @ResultMap("productResultMap") 102 | List page(@Param("pageNumber")int pageNumber,@Param("pageSize")int pageSize); 103 | 104 | 105 | 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/model/Category.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.model; 2 | 3 | import java.io.Serializable; 4 | import java.util.ArrayList; 5 | 6 | import javax.xml.bind.annotation.XmlElement; 7 | import javax.xml.bind.annotation.XmlRootElement; 8 | 9 | @SuppressWarnings("serial") 10 | @XmlRootElement(name="category") 11 | public class Category implements Serializable{ 12 | 13 | private int id; 14 | private String name; 15 | private ArrayList products = new ArrayList(); 16 | 17 | public int getId() { 18 | return id; 19 | } 20 | @XmlElement(name="categoryid") 21 | public void setId(int id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | @XmlElement(name="categoryname") 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | public ArrayList getProducts() { 34 | return products; 35 | } 36 | 37 | public void setProducts(ArrayList products) { 38 | this.products = products; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "Category [id=" + id + ", name=" + name + ", products=" 44 | + products + "]"; 45 | } 46 | 47 | 48 | public Category() { 49 | } 50 | 51 | public Category(String name) { 52 | super(); 53 | this.name = name; 54 | } 55 | 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/model/Product.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/model/Product.java -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/model/Products.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.model; 2 | 3 | import java.util.List; 4 | 5 | import javax.xml.bind.annotation.XmlElement; 6 | import javax.xml.bind.annotation.XmlRootElement; 7 | 8 | @XmlRootElement(name="products") 9 | public class Products { 10 | 11 | private List products; 12 | 13 | public List getProducts() { 14 | return products; 15 | } 16 | 17 | @XmlElement(name="product") 18 | public void setProducts(List products) { 19 | this.products = products; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/service/CategoryService.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.service; 2 | 3 | import java.util.List; 4 | 5 | import org.mybatis.spring.model.Category; 6 | 7 | public interface CategoryService { 8 | 9 | 10 | List listAllCategory(); 11 | 12 | 13 | Category selectCategoryById(int id); 14 | 15 | void saveCategory(Category category); 16 | 17 | 18 | void updateCategory(Category category); 19 | 20 | Category findCategoryById(int id); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/service/ProductService.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/service/ProductService.java -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/service/impl/CategoryServiceImpl.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.mybatis.spring.mapper.CategoryMapper; 6 | import org.mybatis.spring.model.Category; 7 | import org.mybatis.spring.service.CategoryService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.transaction.annotation.Propagation; 11 | import org.springframework.transaction.annotation.Transactional; 12 | 13 | @Service("categoryServiceImpl") 14 | @Transactional(propagation=Propagation.REQUIRED) 15 | public class CategoryServiceImpl implements CategoryService { 16 | 17 | @Autowired 18 | private CategoryMapper categoryMapper; 19 | 20 | @Transactional(propagation=Propagation.REQUIRED,readOnly=true) 21 | public List listAllCategory() { 22 | return categoryMapper.listAllCategory(); 23 | } 24 | 25 | public Category selectCategoryById(int id) { 26 | return categoryMapper.selectCategoryById(id); 27 | } 28 | 29 | public void saveCategory(Category category) { 30 | categoryMapper.saveCategory(category); 31 | } 32 | 33 | public void updateCategory(Category category) { 34 | categoryMapper.updateCategory(category); 35 | } 36 | 37 | public Category findCategoryById(int id) { 38 | return categoryMapper.findCategoryById(id); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/service/impl/ProductServiceImpl.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import org.mybatis.spring.mapper.ProductMapper; 6 | import org.mybatis.spring.model.Product; 7 | import org.mybatis.spring.service.ProductService; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Service; 10 | import org.springframework.transaction.annotation.Propagation; 11 | import org.springframework.transaction.annotation.Transactional; 12 | 13 | @Service("productServiceImpl") 14 | @Transactional(propagation=Propagation.REQUIRED) 15 | public class ProductServiceImpl implements ProductService { 16 | 17 | @Autowired 18 | private ProductMapper productMapper; 19 | 20 | 21 | 22 | @Transactional(propagation=Propagation.REQUIRED) 23 | public List listAllProduct() { 24 | return productMapper.listAllProduct(); 25 | } 26 | 27 | @Transactional(propagation=Propagation.REQUIRED) 28 | public List listProductByCategoryid(int categoryid) { 29 | return productMapper.listProductByCategoryid(categoryid); 30 | } 31 | 32 | @Transactional(propagation=Propagation.REQUIRED) 33 | public List listAllProductIncludeCategoryName() { 34 | return productMapper.listAllProductIncludeCategoryName(); 35 | } 36 | 37 | public void insertProduct(Product product) { 38 | productMapper.insertProduct(product); 39 | } 40 | 41 | public void updateProduct(Product product) { 42 | productMapper.updateProduct(product); 43 | } 44 | 45 | public Product findProductById(int id) { 46 | return productMapper.findProductById(id); 47 | } 48 | 49 | public List like(String name) { 50 | return productMapper.like("%"+name+"%"); 51 | } 52 | 53 | public List page(int pageNumber, int pageSize) { 54 | return productMapper.page(pageNumber, pageSize); 55 | } 56 | 57 | public List dynaQuerySql(Product product) { 58 | return productMapper.dynaQuerySql(product); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/spring/utils/UTF8StringHttpMessageConverter.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/java/org/mybatis/spring/utils/UTF8StringHttpMessageConverter.java -------------------------------------------------------------------------------- /src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /src/main/resources/jdbc.properties: -------------------------------------------------------------------------------- 1 | driverClassName=com.p6spy.engine.spy.P6SpyDriver 2 | url=jdbc:mysql://localhost:3306/mybatis 3 | username=root 4 | password= -------------------------------------------------------------------------------- /src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 2 | log4j.appender.stdout.Target=System.out 3 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 4 | log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %50.50c:%4L - %m%n 5 | 6 | log4j.rootLogger=WARN, stdout 7 | 8 | log4j.logger.org.springframework.data=INFO 9 | #log4j.logger.org.springframework.jdbc=DEBUG 10 | 11 | 12 | #sql and execute time 13 | #log4j.logger.jdbc.sqltiming=INFO 14 | 15 | #sql only 16 | log4j.logger.jdbc.sqlonly=INFO -------------------------------------------------------------------------------- /src/main/resources/mybatis-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/main/resources/org/mybatis/spring/mapper/CategoryMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 23 | 24 | 25 | 26 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/resources/org/mybatis/spring/mapper/ProductMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 21 | 22 | 23 | 24 | 25 | 26 | 29 | 30 | 31 | 32 | 35 | 36 | 37 | 38 | 41 | 42 | 43 | 44 | 51 | 52 | 53 | 54 | 87 | 88 | -------------------------------------------------------------------------------- /src/main/resources/spy.properties: -------------------------------------------------------------------------------- 1 | ################################################################# 2 | # P6Spy Options File # 3 | # See documentation for detailed instructions # 4 | ################################################################# 5 | 6 | ################################################################# 7 | # MODULES # 8 | # # 9 | # Modules provide the P6Spy functionality. If a module, such # 10 | # as module_log is commented out, that functionality will not # 11 | # be available. If it is not commented out (if it is active), # 12 | # the functionality will be active. # 13 | # # 14 | # Values set in Modules cannot be reloaded using the # 15 | # reloadproperties variable. Once they are loaded, they remain # 16 | # in memory until the application is restarted. # 17 | # # 18 | ################################################################# 19 | 20 | module.log=com.p6spy.engine.logging.P6LogFactory 21 | #module.outage=com.p6spy.engine.outage.P6OutageFactory 22 | 23 | ################################################################# 24 | # REALDRIVER(s) # 25 | # # 26 | # In your application server configuration file you replace the # 27 | # "real driver" name with com.p6spy.engine.P6SpyDriver. This is # 28 | # where you put the name of your real driver P6Spy can find and # 29 | # register your real driver to do the database work. # 30 | # # 31 | # If your application uses several drivers specify them in # 32 | # realdriver2, realdriver3. See the documentation for more # 33 | # details. # 34 | # # 35 | # Values set in REALDRIVER(s) cannot be reloaded using the # 36 | # reloadproperties variable. Once they are loaded, they remain # 37 | # in memory until the application is restarted. # 38 | # # 39 | ################################################################# 40 | 41 | # oracle driver 42 | # realdriver=oracle.jdbc.driver.OracleDriver 43 | 44 | # mysql Connector/J driver 45 | # realdriver=com.mysql.jdbc.Driver 46 | 47 | # informix driver 48 | # realdriver=com.informix.jdbc.IfxDriver 49 | 50 | # ibm db2 driver 51 | # realdriver=COM.ibm.db2.jdbc.net.DB2Driver 52 | 53 | # the mysql open source driver 54 | realdriver=com.mysql.jdbc.Driver 55 | 56 | #specifies another driver to use 57 | realdriver2= 58 | #specifies a third driver to use 59 | realdriver3= 60 | 61 | 62 | #the DriverManager class sequentially tries every driver that is 63 | #registered to find the right driver. In some instances, it's possible to 64 | #load up the realdriver before the p6spy driver, in which case your connections 65 | #will not get wrapped as the realdriver will "steal" the connection before 66 | #p6spy sees it. Set the following property to "true" to cause p6spy to 67 | #explicitily deregister the realdrivers 68 | deregisterdrivers=true 69 | 70 | ################################################################ 71 | # P6LOG SPECIFIC PROPERTIES # 72 | ################################################################ 73 | # no properties currently available 74 | 75 | ################################################################ 76 | # EXECUTION THRESHOLD PROPERTIES # 77 | ################################################################ 78 | # This feature applies to the standard logging of P6Spy. # 79 | # While the standard logging logs out every statement # 80 | # regardless of its execution time, this feature puts a time # 81 | # condition on that logging. Only statements that have taken # 82 | # longer than the time specified (in milliseconds) will be # 83 | # logged. This way it is possible to see only statements that # 84 | # have exceeded some high water mark. # 85 | # This time is reloadable. # 86 | # 87 | # executionthreshold=integer time (milliseconds) 88 | # 89 | executionthreshold= 90 | 91 | ################################################################ 92 | # P6OUTAGE SPECIFIC PROPERTIES # 93 | ################################################################ 94 | # Outage Detection 95 | # 96 | # This feature detects long-running statements that may be indicative of 97 | # a database outage problem. If this feature is turned on, it will log any 98 | # statement that surpasses the configurable time boundary during its execution. 99 | # When this feature is enabled, no other statements are logged except the long 100 | # running statements. The interval property is the boundary time set in seconds. 101 | # For example, if this is set to 2, then any statement requiring at least 2 102 | # seconds will be logged. Note that the same statement will continue to be logged 103 | # for as long as it executes. So if the interval is set to 2, and the query takes 104 | # 11 seconds, it will be logged 5 times (at the 2, 4, 6, 8, 10 second intervals). 105 | # 106 | # outagedetection=true|false 107 | # outagedetectioninterval=integer time (seconds) 108 | # 109 | outagedetection=false 110 | outagedetectioninterval= 111 | 112 | ################################################################ 113 | # COMMON PROPERTIES # 114 | ################################################################ 115 | 116 | # filter what is logged 117 | filter=false 118 | 119 | # comma separated list of tables to include when filtering 120 | include = 121 | # comma separated list of tables to exclude when filtering 122 | exclude = 123 | 124 | # sql expression to evaluate if using regex filtering 125 | sqlexpression = 126 | 127 | 128 | # turn on tracing 129 | autoflush = true 130 | 131 | # sets the date format using Java's SimpleDateFormat routine 132 | dateformat= 133 | 134 | #list of categories to explicitly include 135 | includecategories= 136 | 137 | #list of categories to exclude: error, info, batch, debug, statement, 138 | #commit, rollback and result are valid values 139 | excludecategories=info,debug,result,batch 140 | 141 | 142 | #allows you to use a regex engine or your own matching engine to determine 143 | #which statements to log 144 | # 145 | #stringmatcher=com.p6spy.engine.common.GnuRegexMatcher 146 | #stringmatcher=com.p6spy.engine.common.JakartaRegexMatcher 147 | stringmatcher= 148 | 149 | # prints a stack trace for every statement logged 150 | stacktrace=false 151 | # if stacktrace=true, specifies the stack trace to print 152 | stacktraceclass= 153 | 154 | # determines if property file should be reloaded 155 | reloadproperties=false 156 | # determines how often should be reloaded in seconds 157 | reloadpropertiesinterval=60 158 | 159 | #if=true then url must be prefixed with p6spy: 160 | useprefix=false 161 | 162 | #specifies the appender to use for logging 163 | #appender=com.p6spy.engine.logging.appender.Log4jLogger 164 | #appender=com.p6spy.engine.logging.appender.StdoutLogger 165 | appender=com.p6spy.engine.logging.appender.FileLogger 166 | 167 | # name of logfile to use, note Windows users should make sure to use forward slashes in their pathname (e:/test/spy.log) (used for file logger only) 168 | logfile = spy.log 169 | 170 | # append to the p6spy log file. if this is set to false the 171 | # log file is truncated every time. (file logger only) 172 | append=true 173 | 174 | #The following are for log4j logging only 175 | log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender 176 | log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout 177 | log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n 178 | 179 | #log4j.appender.CHAINSAW_CLIENT=org.apache.log4j.net.SocketAppender 180 | #log4j.appender.CHAINSAW_CLIENT.RemoteHost=localhost 181 | #log4j.appender.CHAINSAW_CLIENT.Port=4445 182 | #log4j.appender.CHAINSAW_CLIENT.LocationInfo=true 183 | 184 | log4j.logger.p6spy=INFO,STDOUT 185 | 186 | 187 | ################################################################# 188 | # DataSource replacement # 189 | # # 190 | # Replace the real DataSource class in your application server # 191 | # configuration with the name com.p6spy.engine.spy.P6DataSource,# 192 | # then add the JNDI name and class name of the real # 193 | # DataSource here # 194 | # # 195 | # Values set in this item cannot be reloaded using the # 196 | # reloadproperties variable. Once it is loaded, it remains # 197 | # in memory until the application is restarted. # 198 | # # 199 | ################################################################# 200 | #realdatasource=/RealMySqlDS 201 | #realdatasourceclass=com.mysql.jdbc.jdbc2.optional.MysqlDataSource 202 | 203 | ################################################################# 204 | # DataSource properties # 205 | # # 206 | # If you are using the DataSource support to intercept calls # 207 | # to a DataSource that requires properties for proper setup, # 208 | # define those properties here. Use name value pairs, separate # 209 | # the name and value with a semicolon, and separate the # 210 | # pairs with commas. # 211 | # # 212 | # The example shown here is for mysql # 213 | # # 214 | ################################################################# 215 | #realdatasourceproperties=port;3306,serverName;ibmhost,databaseName;mydb 216 | 217 | 218 | ################################################################# 219 | # JNDI DataSource lookup # 220 | # # 221 | # If you are using the DataSource support outside of an app # 222 | # server, you will probably need to define the JNDI Context # 223 | # environment. # 224 | # # 225 | # If the P6Spy code will be executing inside an app server then # 226 | # do not use these properties, and the DataSource lookup will # 227 | # use the naming context defined by the app server. # 228 | # # 229 | # The two standard elements of the naming environment are # 230 | # jndicontextfactory and jndicontextproviderurl. If you need # 231 | # additional elements, use the jndicontextcustom property. # 232 | # You can define multiple properties in jndicontextcustom, # 233 | # in name value pairs. Separate the name and value with a # 234 | # semicolon, and separate the pairs with commas. # 235 | # # 236 | # The example shown here is for a standalone program running on # 237 | # a machine that is also running JBoss, so the JDNI context # 238 | # is configured for JBoss (3.0.4). # 239 | # # 240 | ################################################################# 241 | #jndicontextfactory=org.jnp.interfaces.NamingContextFactory 242 | #jndicontextproviderurl=localhost:1099 243 | #jndicontextcustom=java.naming.factory.url.pkgs;org.jboss.nameing:org.jnp.interfaces 244 | 245 | #jndicontextfactory=com.ibm.websphere.naming.WsnInitialContextFactory 246 | #jndicontextproviderurl=iiop://localhost:900 -------------------------------------------------------------------------------- /src/main/resources/views.properties: -------------------------------------------------------------------------------- 1 | # And a sample properties file is uses (views.properties in WEB-INF/classes): 2 | velocity.(class)=org.springframework.web.servlet.view.velocity.VelocityView 3 | velocity.url=velocity.vm 4 | 5 | freemarker.(class)=org.springframework.web.servlet.view.freemarker.FreeMarkerView 6 | freemarker.url=freemarker.ftl -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/dispatche-servlet.xml: -------------------------------------------------------------------------------- 1 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.mybatis.spring.model.Product 37 | org.mybatis.spring.model.Products 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | simpleUrlController 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | text/plain;charset=UTF-8 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/freemarker/freemarker.ftl: -------------------------------------------------------------------------------- 1 | ${message} 2 |
3 |

This Message is from the freemarker view !

-------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/velocity.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/main/webapp/WEB-INF/velocity.properties -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/velocity/velocity.vm: -------------------------------------------------------------------------------- 1 | $!{message} 2 |
3 |

This Message is from the velocity view !

-------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/index.jsp: -------------------------------------------------------------------------------- 1 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 2 | 4 | 5 | 6 | 7 | 8 | JSP Page 9 | 10 | 11 |

Hello World!

12 | 13 | 14 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/showuser.jsp: -------------------------------------------------------------------------------- 1 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 2 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 | 5 | 6 | 7 | 8 | 基于REST风格的请求 9 | 10 | 11 |

12 |
13 |

This view page is from /hello/showUser/${username}/${pwd } !

14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/views/welcome.jsp: -------------------------------------------------------------------------------- 1 | <%@page contentType="text/html" pageEncoding="UTF-8"%> 2 | <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 3 | 5 | 6 | 7 | 8 | JSP Page 9 | 10 | 11 |

12 |
13 |

This Message is from the jsp view !

14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | mybatis-spring 4 | 5 | 6 | 7 | org.springframework.web.context.ContextLoaderListener 8 | 9 | 10 | 11 | contextConfigLocation 12 | classpath:applicationContext.xml 13 | 14 | 15 | 16 | 17 | dispatcher 18 | org.springframework.web.servlet.DispatcherServlet 19 | 20 | contextConfigLocation 21 | /WEB-INF/dispatche-servlet.xml 22 | 23 | 24 | 25 | 26 | dispatcher 27 | *.do 28 | 29 | 30 | 31 | 32 | 33 | 34 | 30 35 | 36 | 37 | 38 | index.jsp 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/service/impl/CategoryServiceImplTest.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/v5tech/mybatis-spring/71ac3a3aadb98e7e09d559f58c6d1e8e821daa8a/src/test/java/org/mybatis/spring/service/impl/CategoryServiceImplTest.java -------------------------------------------------------------------------------- /src/test/java/org/mybatis/spring/service/impl/ProductServiceImplTest.java: -------------------------------------------------------------------------------- 1 | package org.mybatis.spring.service.impl; 2 | 3 | import java.util.List; 4 | 5 | import javax.annotation.Resource; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | import org.mybatis.spring.model.Product; 10 | import org.mybatis.spring.service.ProductService; 11 | import org.springframework.test.context.ContextConfiguration; 12 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 13 | 14 | @ContextConfiguration("classpath:applicationContext.xml") 15 | @RunWith(SpringJUnit4ClassRunner.class) 16 | public class ProductServiceImplTest { 17 | 18 | @Resource(name="productServiceImpl") 19 | private ProductService productService; 20 | 21 | @Test 22 | public void listAllProduct(){ 23 | List listAllProduct = productService.listAllProduct(); 24 | for (Product product : listAllProduct) { 25 | System.out.println(product); 26 | } 27 | } 28 | 29 | 30 | @Test 31 | public void listProductByCategoryid(){ 32 | List listAllProduct = productService.listProductByCategoryid(2); 33 | for (Product product : listAllProduct) { 34 | System.out.println(product); 35 | } 36 | } 37 | 38 | 39 | @Test 40 | public void listAllProductIncludeCategoryName(){ 41 | List listAllProduct = productService.listAllProductIncludeCategoryName(); 42 | for (Product product : listAllProduct) { 43 | System.out.println(product); 44 | } 45 | } 46 | 47 | @Test 48 | public void like(){ 49 | List listAllProduct = productService.like("ipa"); 50 | for (Product product : listAllProduct) { 51 | System.out.println(product); 52 | } 53 | } 54 | 55 | 56 | @Test 57 | public void page(){ 58 | List listAllProduct = productService.page(0, 2); 59 | for (Product product : listAllProduct) { 60 | System.out.println(product); 61 | } 62 | } 63 | 64 | @Test 65 | public void dynaQuerySql(){ 66 | List listAllProduct = productService.dynaQuerySql(new Product("ipad", 3000.0f, null, 1)); 67 | for (Product product : listAllProduct) { 68 | System.out.println(product); 69 | } 70 | } 71 | 72 | 73 | @Test 74 | public void insertProduct(){ 75 | productService.insertProduct(new Product("Iphone5", 5000, "据听说iphone5很丑啊", 2)); 76 | } 77 | 78 | 79 | @Test 80 | public void findProductById(){ 81 | Product product = productService.findProductById(1); 82 | System.out.println(product); 83 | } 84 | 85 | 86 | @Test 87 | public void updateProduct(){ 88 | 89 | Product product=productService.findProductById(17); 90 | product.setDescription("iphone5很丑很贵没有肾了买不起了怎么办啊"); 91 | product.setPrice(8000); 92 | productService.updateProduct(product); 93 | 94 | 95 | } 96 | 97 | 98 | } 99 | --------------------------------------------------------------------------------