diff --git a/Chapter3-2-7/pom.xml b/Chapter3-2-7/pom.xml new file mode 100644 index 0000000..ba19038 --- /dev/null +++ b/Chapter3-2-7/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.didispace + Chapter3-2-7 + 1.0.0 + jar + + Chapter3-2-7 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.1.1 + + + + mysql + mysql-connector-java + 5.1.21 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter3-2-7/src/main/java/com/didispace/Application.java b/Chapter3-2-7/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..2a14b75 --- /dev/null +++ b/Chapter3-2-7/src/main/java/com/didispace/Application.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter3-2-7/src/main/java/com/didispace/domain/User.java b/Chapter3-2-7/src/main/java/com/didispace/domain/User.java new file mode 100644 index 0000000..2348d54 --- /dev/null +++ b/Chapter3-2-7/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,33 @@ +package com.didispace.domain; + +public class User { + + private Long id; + private String name; + private Integer age; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + +} diff --git a/Chapter3-2-7/src/main/java/com/didispace/domain/UserMapper.java b/Chapter3-2-7/src/main/java/com/didispace/domain/UserMapper.java new file mode 100644 index 0000000..0b95b12 --- /dev/null +++ b/Chapter3-2-7/src/main/java/com/didispace/domain/UserMapper.java @@ -0,0 +1,14 @@ +package com.didispace.domain; + +import org.apache.ibatis.annotations.*; + +@Mapper +public interface UserMapper { + + @Select("SELECT * FROM USER WHERE NAME = #{name}") + User findByName(@Param("name") String name); + + @Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})") + int insert(@Param("name") String name, @Param("age") Integer age); + +} \ No newline at end of file diff --git a/Chapter3-2-7/src/main/resources/application.properties b/Chapter3-2-7/src/main/resources/application.properties new file mode 100644 index 0000000..7aa1af4 --- /dev/null +++ b/Chapter3-2-7/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.datasource.url=jdbc:mysql://localhost:3306/test +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver \ No newline at end of file diff --git a/Chapter3-2-7/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-2-7/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..bba7b8b --- /dev/null +++ b/Chapter3-2-7/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,30 @@ +package com.didispace; + +import com.didispace.domain.User; +import com.didispace.domain.UserMapper; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Autowired + private UserMapper userMapper; + + @Test + @Rollback + public void findByName() throws Exception { + userMapper.insert("AAA", 20); + User u = userMapper.findByName("AAA"); + Assert.assertEquals(20, u.getAge().intValue()); + } + +}