XML中的通用代码模板
<?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">
项目列表:
新增
Controller:
package com.example.springboot.Controller;
import com.example.springboot.Common.User;
import com.example.springboot.Service.UserService;
import com.example.springboot.Util.Page;
import com.example.springboot.Util.R;
import com.example.springboot.Util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
@CrossOrigin("http://localhost:8082") //跨域请求
public class UserController {
@Autowired
private UserService userService;
//插入用户信息
@RequestMapping("/userInsert")
public Result<?> insertUser( User user){
userService.insertUser(user);
return Result.sucess();
}
}
Service中(接口):
package com.example.springboot.Service;
import com.example.springboot.Common.User;
public interface UserService {
void insertUser(User user);
}
ServiceImpl中:
package com.example.springboot.Service.ServiceImpl;
import com.example.springboot.Common.User;
import com.example.springboot.Mapper.UserMapper;
import com.example.springboot.Service.UserService;
import com.example.springboot.Util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void insertUser(User user) {
System.out.println("插入用户");
userMapper.insertUser(user);
}
}
Mapper中:
package com.example.springboot.Mapper;
import com.example.springboot.Common.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface UserMapper {
// 插入用户
void insertUser(User user);
}
MapperXml中:
<?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.example.springboot.Mapper.UserMapper">
<insert id="insertUser" parameterType="com.example.springboot.Common.User">
insert into user(username,usernick,userpasd) values (#{username},#{usernick},#{userpasd})
</insert>
</mapper>
user实体类:
package com.example.springboot.Common;
public class User {
private int id;
private String username;
private String userpasd;
private String usernick;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", userpasd='" + userpasd + '\'' +
", usernick='" + usernick + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpasd() {
return userpasd;
}
public void setUserpasd(String userpasd) {
this.userpasd = userpasd;
}
public String getUsernick() {
return usernick;
}
public void setUsernick(String usernick) {
this.usernick = usernick;
}
public User(){}
public User(int id, String username, String userpasd, String usernick) {
this.id = id;
this.username = username;
this.userpasd = userpasd;
this.usernick = usernick;
}
}