作为一个刚入职实习生,公司老总给的考核就是这个,来这边写出来也是把自己工作的一个情况记录一番!!
刚拿到手,说实话比较懵,因为我基础很差,但通过不断的百度,最后的功能算是实现出来了。
总体效果是这样的!
实体类代码如下,这里的是各种属性,根据自己的需求来!
private String Column1;
private String Column2;
private String Column3;
private String Column4;
private String Column5;
private String Column6;
private String Column7;
private String Column8;
private String Column9;
mapper层,这里是写这接口里的方法``
public interface UserMapper {
Integer insert(User user);//增加
Integer deleteByColumn1(String id);//删除
User findByColumn2(String Column2);
List<User> selectAll();//查询数据库里的所有
Integer updateInfoByColumn1(User user);//修改
}
xml文件- userMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="aaa.mapper.UserMapper">
<resultMap type="aaa.entity.User" id="UserEntityMap">
<id column="Column_1" property="Column1" />
<result column="Column_2" property="Column2" />
<result column="Column_3" property="Column3" />
<result column="Column_4" property="Column4" />
<result column="Column_5" property="Column5" />
<result column="Column_6" property="Column6" />
<result column="Column_7" property="Column7" />
<result column="Column_8" property="Column8" />
<result column="Column_9" property="Column9" />
</resultMap>
<!-- 插入用户数据 -->
<!-- Integer insert(User user); -->
<insert id="insert" >
INSERT
INTO Table_1(
Column_1,
Column_2, Column_3, Column_4,
Column_5, Column_6, Column_7,
Column_8,Column_9
)
VALUES (
#{Column1},
#{Column2}, #{Column3}, #{Column4},
#{Column5},
#{Column6}, #{Column7}, #{Column8},
#{Column9}
)
</insert>
<!-- 根据用户名查询用户数据 -->
<!-- User findByUsername(String username) -->
<select id="findByColumn2" resultMap="UserEntityMap">
SELECT * FROM Table_1 WHERE
Column_2=#{Column2}
</select>
<update id="updateInfoByColumn1">
UPDATE
Table_1
SET
Column_2=#{Column2},
Column_3=#{Column3},
Column_4=#{Column4},
Column_5=#{Column5},
Column_6=#{Column6},
Column_7=#{Column7},
Column_8=#{Column8},
Column_9=#{Column9}
WHERE
Column_1=#{Column1}
</update>
<select id="selectAll" resultMap="UserEntityMap">
SELECT * FROM Table_1
</select>
<delete id="deleteByColumn1">
DELETE FROM Table_1 WHERE Column_1=#{Column1}
</delete>
</mapper>
userService接口
public interface UserService {
void add(String Column_1,String Column_2, String Column_3, String Column_4, String Column_5, String Column_6, String Column_7, String Column_8, String Column_9 );
Object delete(String id );
public List<User> selectAllUser();
Object addUser(User user);
Integer insert(User user);
void changeInfo(String Column1, String Column2, String Column3,
String Column4, String Column5, String Column6, String Column7,
String Column8, String Column9);
//分页查询所有数据
List<User> getMessageList(int page, int rows);
Integer findCount();
}
impl层
@Service
public class UserServiceImpl implements UserService {
@Resource
@Autowired
private UserMapper userMapper;
Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
@Override
public Object delete(String id) {
Integer deletResult= userMapper.deleteByColumn1(id);
if(deletResult<1){
return 2;
}
return 100;
}
@Override
public List<User> selectAllUser() {
List<User> list=userMapper.selectAll();
return list;
}
@Override
public Object addUser(User user) {
Integer insertResult= userMapper.insert(user);
if(insertResult<1){
return 1;
}
return 2;
}
@Override
public void add(String Column1,String Column2, String Column3, String Column4,
String Column5, String Column6, String Column7, String Column8,
String Column9) {
System.err.println("开始新增");
User user = new User();
user.setColumn1(Column1);
user.setColumn2(Column2);
user.setColumn3(Column3);
user.setColumn4(Column4);
user.setColumn5(Column5);
user.setColumn6(Column6);
user.setColumn7(Column7);
user.setColumn8(Column8);
user.setColumn9(Column9);
userMapper.insert(user);
}
@Override
public Integer insert(User user) {
int rows =userMapper.insert(user);
return rows;
}
@Override
public void changeInfo(String Column1, String Column2, String Column3,
String Column4, String Column5, String Column6, String Column7,
String Column8, String Column9) {
System.err.println(Column7+":"+Column5+":"+Column1);
User user =new User();
user.setColumn1(Column1);
user.setColumn2(Column2);
user.setColumn3(Column3);
user.setColumn4(Column4);
user.setColumn5(Column5);
user.setColumn6(Column6);
user.setColumn7(Column7);
user.setColumn8(Column8);
user.setColumn9(Column9);
userMapper.updateInfoByColumn1(user);
}
controller
@Autowired
private UserService userService;
@RequestMapping("/login")
public String insert(User user) {
if(Util.isEmail(user.getColumn9())&&Util.IdCard(user.getColumn6())&&Util.phone(user.getColumn8())&&Util.isChinese(user.getColumn3())){
String Column_1=UUID.randomUUID().toString().replace("-", "");
userService.add(Column_1,user.getColumn2(), user.getColumn3(), user.getColumn4(), user.getColumn5(), user.getColumn6(), user.getColumn7(), user.getColumn8(),user.getColumn9());
return "添加成功";
}else if(!Util.isChinese(user.getColumn6())){
return "姓名格式错误";
}else if(!Util.IdCard(user.getColumn6())){
return "身份证格式错误";
}else if(!Util.phone(user.getColumn8())){
return "手机格式错误";
}else if(!Util.isEmail(user.getColumn9())){
return "邮箱格式格式错误";
}
return "添加失败";
/*String Column_1=UUID.randomUUID().toString().replace("-", "");
userService.add(Column_1,user.getColumn2(), user.getColumn3(), user.getColumn4(), user.getColumn5(), user.getColumn6(), user.getColumn7(), user.getColumn8(),user.getColumn9());
return "添加成功";*/
}
@RequestMapping("/selectAllUser")
public Object selectAllUser(){
return userService.selectAllUser();
}
@RequestMapping("/delete")
public Object deleteUser(String id){
System.err.println(id);
String[] temp =id.split(",");
for(int i =0; i < temp.length ; i++){
System.out.println(temp[i]);
userService.delete(temp[i]);
}
System.err.println("已删除");
return "删除成功";
}
@RequestMapping("/update")
public String update(User user){
if(Util.isEmail(user.getColumn9())&&Util.IdCard(user.getColumn6())&&Util.phone(user.getColumn8())&&Util.isChinese(user.getColumn3())){
userService.changeInfo(user.getColumn1(),user.getColumn2(), user.getColumn3(), user.getColumn4(), user.getColumn5(), user.getColumn6(), user.getColumn7(), user.getColumn8(),user.getColumn9());
return "添加成功";
}else if(!Util.isChinese(user.getColumn3())){
return "姓名格式错误";
}else if(!Util.IdCard(user.getColumn6())){
return "身份证格式错误";
}else if(!Util.phone(user.getColumn8())){
return "手机格式错误";
}else if(!Util.isEmail(user.getColumn9())){
return "邮箱格式格式错误";
}
return "添加失败";
}
到这里,代码已经写完了,把页面放在下一篇文章,欢迎各位大神指出不足之处