diff --git a/Chapter3-2-8/pom.xml b/Chapter3-2-8/pom.xml new file mode 100644 index 0000000..0049b2b --- /dev/null +++ b/Chapter3-2-8/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.didispace + Chapter3-2-8 + 1.0.0 + jar + + Chapter3-2-8 + 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-8/src/main/java/com/didispace/Application.java b/Chapter3-2-8/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..2a14b75 --- /dev/null +++ b/Chapter3-2-8/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-8/src/main/java/com/didispace/domain/User.java b/Chapter3-2-8/src/main/java/com/didispace/domain/User.java new file mode 100644 index 0000000..cfcc47d --- /dev/null +++ b/Chapter3-2-8/src/main/java/com/didispace/domain/User.java @@ -0,0 +1,47 @@ +package com.didispace.domain; + +public class User { + + private Long id; + private String name; + private Integer age; + + public User() { + } + + public User(Long id, String name, Integer age) { + this.id = id; + this.name = name; + this.age = age; + } + + public User(String name, Integer age) { + this.name = name; + this.age = 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-8/src/main/java/com/didispace/domain/UserMapper.java b/Chapter3-2-8/src/main/java/com/didispace/domain/UserMapper.java new file mode 100644 index 0000000..b0d82e4 --- /dev/null +++ b/Chapter3-2-8/src/main/java/com/didispace/domain/UserMapper.java @@ -0,0 +1,36 @@ +package com.didispace.domain; + +import org.apache.ibatis.annotations.*; + +import java.util.List; +import java.util.Map; + +@Mapper +public interface UserMapper { + + @Select("SELECT * FROM user WHERE name = #{name}") + User findByName(@Param("name") String name); + + @Results({ + @Result(property = "name", column = "name"), + @Result(property = "age", column = "age") + }) + @Select("SELECT name, age FROM user") + List findAll(); + + @Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})") + int insert(@Param("name") String name, @Param("age") Integer age); + + @Update("UPDATE user SET age=#{age} WHERE name=#{name}") + void update(User user); + + @Delete("DELETE FROM user WHERE id =#{id}") + void delete(Long id); + + @Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})") + int insertByUser(User user); + + @Insert("INSERT INTO user(name, age) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})") + int insertByMap(Map map); + +} \ No newline at end of file diff --git a/Chapter3-2-8/src/main/resources/application.properties b/Chapter3-2-8/src/main/resources/application.properties new file mode 100644 index 0000000..7aa1af4 --- /dev/null +++ b/Chapter3-2-8/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-8/src/test/java/com/didispace/ApplicationTests.java b/Chapter3-2-8/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..6a91c2e --- /dev/null +++ b/Chapter3-2-8/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,63 @@ +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; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@Transactional +public class ApplicationTests { + + @Autowired + private UserMapper userMapper; + + @Test + @Rollback + public void testUserMapper() throws Exception { + // insert一条数据,并select出来验证 + userMapper.insert("AAA", 20); + User u = userMapper.findByName("AAA"); + Assert.assertEquals(20, u.getAge().intValue()); + // update一条数据,并select出来验证 + u.setAge(30); + userMapper.update(u); + u = userMapper.findByName("AAA"); + Assert.assertEquals(30, u.getAge().intValue()); + // 删除这条数据,并select验证 + userMapper.delete(u.getId()); + u = userMapper.findByName("AAA"); + Assert.assertEquals(null, u); + + u = new User("BBB", 30); + userMapper.insertByUser(u); + Assert.assertEquals(30, userMapper.findByName("BBB").getAge().intValue()); + + Map map = new HashMap<>(); + map.put("name", "CCC"); + map.put("age", 40); + userMapper.insertByMap(map); + Assert.assertEquals(40, userMapper.findByName("CCC").getAge().intValue()); + + List userList = userMapper.findAll(); + for(User user : userList) { + Assert.assertEquals(null, user.getId()); + Assert.assertNotEquals(null, user.getName()); + } + + } + +}