Skip to content

Commit 893fda4

Browse files
committed
spring-mybatis-junit
spring-mybatis-junit
1 parent 10e9baa commit 893fda4

File tree

14 files changed

+662
-0
lines changed

14 files changed

+662
-0
lines changed

spring-mybatis-junit/.classpath

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/test/resources" output="target/test-classes" excluding="**/*.java"/>
5+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
6+
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
7+
<classpathentry kind="output" path="target/classes"/>
8+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
9+
<classpathentry kind="var" path="M2_REPO/com/h2database/h2/1.3.171/h2-1.3.171.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/junit/junit/4.11/junit-4.11.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/org/mybatis/mybatis/3.2.1/mybatis-3.2.1.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/org/mybatis/mybatis-spring/1.2.0/mybatis-spring-1.2.0.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.1.0.RELEASE/spring-core-3.1.0.RELEASE.jar"/>
15+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-asm/3.1.0.RELEASE/spring-asm-3.1.0.RELEASE.jar"/>
16+
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
17+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.1.0.RELEASE/spring-expression-3.1.0.RELEASE.jar"/>
18+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.1.0.RELEASE/spring-beans-3.1.0.RELEASE.jar"/>
19+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.1.0.RELEASE/spring-aop-3.1.0.RELEASE.jar"/>
20+
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar"/>
21+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.1.0.RELEASE/spring-context-3.1.0.RELEASE.jar"/>
22+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.1.0.RELEASE/spring-context-support-3.1.0.RELEASE.jar"/>
23+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-tx/3.1.0.RELEASE/spring-tx-3.1.0.RELEASE.jar"/>
24+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-jdbc/3.1.0.RELEASE/spring-jdbc-3.1.0.RELEASE.jar"/>
25+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-orm/3.1.0.RELEASE/spring-orm-3.1.0.RELEASE.jar"/>
26+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-oxm/3.1.0.RELEASE/spring-oxm-3.1.0.RELEASE.jar"/>
27+
<classpathentry kind="var" path="M2_REPO/commons-lang/commons-lang/2.5/commons-lang-2.5.jar"/>
28+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-test/3.1.0.RELEASE/spring-test-3.1.0.RELEASE.jar"/>
29+
</classpath>

spring-mybatis-junit/pom.xml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>spring-mybatis-junit</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-mybatis-junit</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<org.springframework.version>3.1.0.RELEASE</org.springframework.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>com.h2database</groupId>
21+
<artifactId>h2</artifactId>
22+
<version>1.3.171</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.11</version>
28+
<scope>test</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.mybatis</groupId>
32+
<artifactId>mybatis</artifactId>
33+
<version>3.2.1</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.mybatis</groupId>
37+
<artifactId>mybatis-spring</artifactId>
38+
<version>1.2.0</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-core</artifactId>
43+
<version>${org.springframework.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework</groupId>
47+
<artifactId>spring-expression</artifactId>
48+
<version>${org.springframework.version}</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework</groupId>
52+
<artifactId>spring-beans</artifactId>
53+
<version>${org.springframework.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.springframework</groupId>
57+
<artifactId>spring-aop</artifactId>
58+
<version>${org.springframework.version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework</groupId>
62+
<artifactId>spring-context</artifactId>
63+
<version>${org.springframework.version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.springframework</groupId>
67+
<artifactId>spring-context-support</artifactId>
68+
<version>${org.springframework.version}</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework</groupId>
72+
<artifactId>spring-tx</artifactId>
73+
<version>${org.springframework.version}</version>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework</groupId>
77+
<artifactId>spring-jdbc</artifactId>
78+
<version>${org.springframework.version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>org.springframework</groupId>
82+
<artifactId>spring-orm</artifactId>
83+
<version>${org.springframework.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.springframework</groupId>
87+
<artifactId>spring-oxm</artifactId>
88+
<version>${org.springframework.version}</version>
89+
</dependency>
90+
91+
<dependency>
92+
<groupId>org.springframework</groupId>
93+
<artifactId>spring-asm</artifactId>
94+
<version>${org.springframework.version}</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>org.springframework</groupId>
98+
<artifactId>spring-context-support</artifactId>
99+
<version>${org.springframework.version}</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>org.springframework</groupId>
103+
<artifactId>spring-test</artifactId>
104+
<version>${org.springframework.version}</version>
105+
<scope>test</scope>
106+
</dependency>
107+
</dependencies>
108+
<build>
109+
<plugins>
110+
<plugin>
111+
<artifactId>maven-compiler-plugin</artifactId>
112+
<version>2.0.2</version>
113+
<configuration>
114+
<source>1.6</source>
115+
<target>1.6</target>
116+
</configuration>
117+
</plugin>
118+
<plugin>
119+
<groupId>org.codehaus.mojo</groupId>
120+
<artifactId>exec-maven-plugin</artifactId>
121+
<executions>
122+
<execution>
123+
<id>my-execution</id>
124+
<phase>compile</phase>
125+
<goals>
126+
<goal>java</goal>
127+
</goals>
128+
</execution>
129+
</executions>
130+
<configuration>
131+
<mainClass>com.hmkcode.spring.mybatis.App</mainClass>
132+
</configuration>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.hmkcode.spring.mybatis;
2+
3+
import java.util.List;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.support.ClassPathXmlApplicationContext;
9+
10+
import com.hmkcode.spring.mybatis.service.Service;
11+
import com.hmkcode.spring.mybatis.vo.Child;
12+
import com.hmkcode.spring.mybatis.vo.Parent;
13+
14+
15+
public class App
16+
{
17+
private static Log log = LogFactory.getLog(App.class);
18+
19+
public static void main( String[] args )
20+
{
21+
22+
ApplicationContext cxt = new ClassPathXmlApplicationContext("spring-config.xml");
23+
Service service = (Service) cxt.getBean("service");
24+
25+
log.info("Running App...");
26+
27+
//( 1 ) SELECT ALL PARENTS
28+
System.out.println("( 1 ) selectAllParent()");
29+
List<Parent> parents = service.selectAllParent();
30+
System.out.println("-> "+parents);
31+
32+
System.out.println("****************************************************");
33+
34+
//( 2 ) SELECT ONE PARENT
35+
System.out.println("( 2 ) selectParent(2)");
36+
Parent parent = service.selectParent(2);
37+
System.out.println("-> "+parent);
38+
39+
System.out.println("****************************************************");
40+
41+
//( 3 ) INSERT ONE PARENT
42+
System.out.println("( 3 ) insertParent(parent)");
43+
parent = new Parent();
44+
parent.setParentName("parent-4 inserted");
45+
service.insertParent(parent);
46+
System.out.println("-> parent inserted");
47+
48+
System.out.println("****************************************************");
49+
50+
//( 4 ) SELECT ALL CHILDREN OF A GIVEN PARENT
51+
System.out.println("( 4 ) selectAllChildren(parentId)");
52+
List<Child> children = service.selectAllChildren(1);
53+
System.out.println("-> "+children);
54+
55+
System.out.println("****************************************************");
56+
57+
//( 5 ) SELECT ONE CHILD
58+
System.out.println("( 5 ) selectChild(1)");
59+
Child child = service.selectChild(1);
60+
System.out.println("-> "+child);
61+
62+
System.out.println("****************************************************");
63+
64+
//( 6 ) INSERT ONE CHILD
65+
System.out.println("( 6 ) selectAllChildren(parentId)");
66+
child = new Child();
67+
child.setChildName("child-4 inserted");
68+
child.setParentId(2);
69+
service.insertchild(child);
70+
System.out.println("-> child inserted");
71+
72+
System.out.println("****************************************************");
73+
74+
//( 7 ) SELECT ALL PARENTS (AGAIN!)
75+
System.out.println("( 7 ) selectAllParent()");
76+
parents = service.selectAllParent();
77+
System.out.println("-> "+parents);
78+
79+
}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.hmkcode.spring.mybatis.mapper;
2+
3+
import java.util.List;
4+
5+
import org.apache.ibatis.annotations.Insert;
6+
import org.apache.ibatis.annotations.Param;
7+
import org.apache.ibatis.annotations.Select;
8+
9+
import com.hmkcode.spring.mybatis.vo.Child;
10+
import com.hmkcode.spring.mybatis.vo.Parent;;
11+
12+
13+
public interface Mapper {
14+
15+
//if @Select is used table/column name and class/property name should be the same
16+
17+
18+
//SQL query in "Mapper.xml"
19+
public List<Parent> selectAllParent();
20+
21+
//SQL query in "Mapper.xml"
22+
public Parent selectParent(@Param("id") int id);
23+
24+
@Insert("INSERT INTO parent (parentName) VALUES (#{parentName})")
25+
public int insertParent(Parent parent);
26+
27+
@Select("SELECT * FROM child WHERE parentId = #{id}")
28+
public List<Child> selectAllChildren(@Param("id") int id);
29+
30+
@Select("SELECT * FROM child WHERE childId = #{id}")
31+
public Child selectChild(@Param("id") int id);
32+
33+
//SQL query in "Mapper.xml"
34+
public int insertchild(Child child);
35+
36+
}
37+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hmkcode.spring.mybatis.service;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
7+
import com.hmkcode.spring.mybatis.mapper.Mapper;
8+
import com.hmkcode.spring.mybatis.vo.Child;
9+
import com.hmkcode.spring.mybatis.vo.Parent;
10+
11+
12+
public class Service {
13+
14+
@Autowired
15+
private Mapper mapper;
16+
17+
public List<Parent> selectAllParent(){
18+
return mapper.selectAllParent();
19+
}
20+
21+
public Parent selectParent(int id){
22+
return mapper.selectParent(id);
23+
}
24+
25+
public int insertParent(Parent parent){
26+
return mapper.insertParent(parent);
27+
}
28+
29+
public List<Child> selectAllChildren(int parnetId){
30+
return mapper.selectAllChildren(parnetId);
31+
}
32+
33+
public Child selectChild(int id){
34+
return mapper.selectChild(id);
35+
}
36+
37+
public int insertchild(Child child){
38+
return mapper.insertchild(child);
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.hmkcode.spring.mybatis.vo;
2+
3+
import org.apache.ibatis.type.Alias;
4+
5+
/**
6+
* Hello world!
7+
*
8+
*/
9+
@Alias("Child")
10+
public class Child extends Parent
11+
{
12+
private int childId;
13+
private String childName;
14+
15+
16+
public int getChildId() {
17+
return childId;
18+
}
19+
public void setChildId(int childId) {
20+
this.childId = childId;
21+
}
22+
public String getChildName() {
23+
return childName;
24+
}
25+
public void setChildName(String childName) {
26+
this.childName = childName;
27+
}
28+
public String toString(){
29+
return "Child [ id: "+this.childId+" - name: "+this.childName+" (PARENT_ID :"+this.getParentId()+") ]";
30+
}
31+
}

0 commit comments

Comments
 (0)