Shanks 7 yıl önce
ebeveyn
işleme
1ac19daf7f

+ 4 - 2
pom.xml

@@ -14,7 +14,7 @@
 	<parent>
 		<groupId>org.springframework.boot</groupId>
 		<artifactId>spring-boot-starter-parent</artifactId>
-		<version>2.0.0.RELEASE</version>
+		<version>1.5.10.RELEASE</version>
 		<relativePath/> <!-- lookup parent from repository -->
 	</parent>
 
@@ -45,7 +45,7 @@
 			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
 		</dependency>
-	</dependencies>
+    </dependencies>
 
 	<build>
 		<plugins>
@@ -57,4 +57,6 @@
 	</build>
 
 
+
+
 </project>

+ 16 - 0
src/main/java/com/ygj/yuemum/ServletInitializer.java

@@ -0,0 +1,16 @@
+package com.ygj.yuemum;
+
+import org.springframework.boot.builder.SpringApplicationBuilder;
+import org.springframework.boot.web.support.SpringBootServletInitializer;
+
+/**
+ * @Author: yf
+ * @Date: 2017/11/27.
+ */
+public class ServletInitializer extends SpringBootServletInitializer {
+
+    @Override
+    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
+        return builder.sources(UserCrudApplication.class);
+    }
+}

+ 14 - 0
src/main/java/com/ygj/yuemum/UserCrudApplication.java

@@ -0,0 +1,14 @@
+package com.ygj.yuemum;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+@MapperScan("com.ygj.yuemum.dao")
+public class UserCrudApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(UserCrudApplication.class, args);
+    }
+}

+ 0 - 12
src/main/java/com/ygj/yuemum/YuemumApplication.java

@@ -1,12 +0,0 @@
-package com.ygj.yuemum;
-
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-@SpringBootApplication
-public class YuemumApplication {
-//12312312312
-	public static void main(String[] args) {
-		SpringApplication.run(YuemumApplication.class, args);
-	}
-}

+ 45 - 0
src/main/java/com/ygj/yuemum/controller/UserController.java

@@ -0,0 +1,45 @@
+package com.ygj.yuemum.controller;
+
+
+import com.ygj.yuemum.pojo.UserPO;
+import com.ygj.yuemum.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+/**
+ * @Author: yf
+ * @Date: 2017/11/27.
+ */
+@RestController
+public class UserController {
+
+    @Autowired
+    private UserService userService;
+
+    @GetMapping("/getUsers")
+    public List<UserPO> getUsers() {
+        return userService.getUsers();
+    }
+
+    @PostMapping("/add")
+    public int add(@ModelAttribute UserPO user) {
+        return userService.addUser(user);
+    }
+
+    @GetMapping("/delete/{id}")
+    public int delete(@PathVariable("id") Integer id) {
+        return userService.deleteUser(id);
+    }
+
+    @PostMapping("/update")
+    public int update(@ModelAttribute UserPO user) {
+        return userService.updateUser(user);
+    }
+
+    @GetMapping("/get/{id}")
+    public UserPO getOne(@PathVariable("id") Integer id) {
+        return  userService.getUser(id);
+    }
+}

+ 44 - 0
src/main/java/com/ygj/yuemum/dao/UserDao.java

@@ -0,0 +1,44 @@
+package com.ygj.yuemum.dao;
+
+import com.ygj.yuemum.pojo.UserPO;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface UserDao {
+
+    /**
+     * 得到所有的用户信息
+     * @return List<UserPO>
+     */
+    List<UserPO> getAll();
+
+    /**
+     * 根据id删除某个用户信息
+     * @param id
+     * @return
+     */
+    int deleteByPrimaryKey(Integer id);
+
+    /**
+     * 插入一条用户信息
+     * @param record
+     * @return
+     */
+    int insertSelective(UserPO record);
+
+    /**
+     * 根据id查找某个用户信息
+     * @param id
+     * @return UserPO
+     */
+    UserPO selectByPrimaryKey(Integer id);
+
+    /**
+     * 根据id更新一条用户信息
+     * @param record
+     * @return
+     */
+    int updateByPrimaryKeySelective(UserPO record);
+}

+ 64 - 0
src/main/java/com/ygj/yuemum/pojo/UserPO.java

@@ -0,0 +1,64 @@
+package com.ygj.yuemum.pojo;
+
+public class UserPO {
+    private Integer id;
+    private String name; // 用户名
+    private String password; // 密码
+    private Integer age; // 年龄
+    private String phone; // 电话号码
+
+    public UserPO() {
+        super();
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name == null ? null : name.trim();
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password == null ? null : password.trim();
+    }
+
+    public Integer getAge() {
+        return age;
+    }
+
+    public void setAge(Integer age) {
+        this.age = age;
+    }
+
+    public String getPhone() {
+        return phone;
+    }
+
+    public void setPhone(String phone) {
+        this.phone = phone == null ? null : phone.trim();
+    }
+
+    @Override
+    public String toString() {
+        return "UserPO{" +
+                "id=" + id +
+                ", name='" + name + '\'' +
+                ", password='" + password + '\'' +
+                ", age=" + age +
+                ", phone='" + phone + '\'' +
+                '}';
+    }
+}

+ 41 - 0
src/main/java/com/ygj/yuemum/service/UserService.java

@@ -0,0 +1,41 @@
+package com.ygj.yuemum.service;
+
+import com.ygj.yuemum.dao.UserDao;
+import com.ygj.yuemum.pojo.UserPO;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * @Author: yf
+ * @Date: 2017/11/27.
+ */
+@Service
+public class UserService {
+
+    @Autowired
+    private UserDao userDao;
+
+    public List<UserPO> getUsers() {
+        List<UserPO> users = userDao.getAll();
+        return users;
+    }
+
+    public int addUser(UserPO user) {
+        return userDao.insertSelective(user);
+    }
+
+    public int deleteUser(Integer id) {
+        return userDao.deleteByPrimaryKey(id);
+    }
+
+    public int updateUser(UserPO user) {
+        return userDao.updateByPrimaryKeySelective(user);
+    }
+
+    public UserPO getUser(Integer id) {
+        return userDao.selectByPrimaryKey(id);
+    }
+}

+ 12 - 8
src/main/resources/application.yml

@@ -1,8 +1,12 @@
-mybatis.config-locations=classpath:mybatis/mybatis-config.xml
-mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
-mybatis.type-aliases-package=com.ygj.yuemum.pojo
-
-spring.datasource.driverClassName = com.mysql.jdbc.Driver
-spring.datasource.url = jdbc:mysql://121.43.235.99:3306/yuemum?useUnicode=true&characterEncoding=utf-8
-spring.datasource.username = jielin
-spring.datasource.password = jielin123
+server:
+  port: 8080
+mybatis:
+  config-location: classpath:mybatis/mybatis-config.xml
+  mapper-locations: classpath:mybatis/mapper/*.xml
+  mybatis.type-aliases-package: com.ygj.yuemum.pojo
+spring:
+  datasource:
+    driverClassName: com.mysql.jdbc.Driver
+    url: jdbc:mysql://121.43.235.99:3306/yuemum?useUnicode=true&characterEncoding=utf-8
+    username: jielin
+    password: jielin123

+ 97 - 0
src/main/resources/mybatis/mapper/UserMapper.xml

@@ -0,0 +1,97 @@
+<?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.ygj.yuemum.dao.UserDao" >
+
+    <resultMap id="BaseResultMap" type="com.ygj.yuemum.pojo.UserPO" >
+        <id column="id" property="id" jdbcType="INTEGER" />
+        <result column="name" property="name" jdbcType="VARCHAR" />
+        <result column="password" property="password" jdbcType="VARCHAR" />
+        <result column="age" property="age" jdbcType="INTEGER" />
+        <result column="phone" property="phone" jdbcType="VARCHAR" />
+    </resultMap>
+
+    <sql id="Base_Column_List" >
+        id, name, password, age, phone
+    </sql>
+
+    <!-- 得到所有的用户信息 -->
+    <select id="getAll" resultMap="BaseResultMap" >
+        select
+        <include refid="Base_Column_List" />
+        from user
+    </select>
+
+    <!-- 根据id查找某个用户信息 -->
+    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
+        select
+        <include refid="Base_Column_List" />
+        from user
+        where id = #{id,jdbcType=INTEGER}
+    </select>
+
+    <!-- 根据id删除某个用户信息 -->
+    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
+        delete from user
+        where id = #{id,jdbcType=INTEGER}
+    </delete>
+
+    <!-- 插入一条用户信息 -->
+    <insert id="insertSelective" parameterType="com.ygj.yuemum.pojo.UserPO" >
+        insert into user
+        <trim prefix="(" suffix=")" suffixOverrides="," >
+            <if test="id != null" >
+                id,
+            </if>
+            <if test="name != null" >
+                name,
+            </if>
+            <if test="password != null" >
+                password,
+            </if>
+            <if test="age != null" >
+                age,
+            </if>
+            <if test="phone != null" >
+                phone,
+            </if>
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides="," >
+            <if test="id != null" >
+                #{id,jdbcType=INTEGER},
+            </if>
+            <if test="name != null" >
+                #{name,jdbcType=VARCHAR},
+            </if>
+            <if test="password != null" >
+                #{password,jdbcType=VARCHAR},
+            </if>
+            <if test="age != null" >
+                #{age,jdbcType=INTEGER},
+            </if>
+            <if test="phone != null" >
+                #{phone,jdbcType=VARCHAR},
+            </if>
+        </trim>
+    </insert>
+
+    <!-- 根据id更新一条用户信息 -->
+    <update id="updateByPrimaryKeySelective" parameterType="com.ygj.yuemum.pojo.UserPO" >
+        update user
+        <set >
+            <if test="name != null" >
+                name = #{name,jdbcType=VARCHAR},
+            </if>
+            <if test="password != null" >
+                password = #{password,jdbcType=VARCHAR},
+            </if>
+            <if test="age != null" >
+                age = #{age,jdbcType=INTEGER},
+            </if>
+            <if test="phone != null" >
+                phone = #{phone,jdbcType=VARCHAR},
+            </if>
+        </set>
+        where id = #{id,jdbcType=INTEGER}
+    </update>
+
+</mapper>

+ 12 - 0
src/main/resources/mybatis/mybatis-config.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
+<configuration>
+    <typeAliases>
+        <typeAlias alias="Integer" type="java.lang.Integer" />
+        <typeAlias alias="Long" type="java.lang.Long" />
+        <typeAlias alias="HashMap" type="java.util.HashMap" />
+        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
+        <typeAlias alias="ArrayList" type="java.util.ArrayList" />
+        <typeAlias alias="LinkedList" type="java.util.LinkedList" />
+    </typeAliases>
+</configuration>