idea增删改查

Posted danxun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了idea增删改查相关的知识,希望对你有一定的参考价值。

 idea应用mybatis写增删改查

    entity层  

private Integer id;
private String userCode;
private String userName;
private String userPassword;
private Integer gender;
private Date birthday;
private String phone;
private String address;
private Integer userRole;
private Integer createdBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserCode() {
return userCode;
}

public void setUserCode(String userCode) {
this.userCode = userCode;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getUserPassword() {
return userPassword;
}

public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public Integer getGender() {
return gender;
}

public void setGender(Integer gender) {
this.gender = gender;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public Integer getUserRole() {
return userRole;
}

public void setUserRole(Integer userRole) {
this.userRole = userRole;
}

public Integer getCreatedBy() {
return createdBy;
}

public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}

public Date getCreationDate() {
return creationDate;
}

public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}

public Integer getModifyBy() {
return modifyBy;
}

public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}

public Date getModifyDate() {
return modifyDate;
}

public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}

    Dao层

      

IUserDao:(Interface
      
//查询
public List<User> getAllUser();
//新增
void addUser(User user);
//删除
void delUser(int id);
//修改
void modifyUser(User user);
//新增时获得主键
void addUserKey(User user);
//模糊查询
public List<User> getAllUsers();


IUserDao:(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">

<!--namespace需要指向接口全路径-->

<mapper namespace="com.qzy.dao.IUserDao">

<!--id代表当前命名空间下(接口下)的唯一方法名 resultType代表返回值类型-->


<select resultType="User" id="getAllUser">
select * from smbms_user
</select>

<insert parameterType="User" id="addUser">
insert into smbms_user values (DEFAULT,#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate},#{modifyBy},#{modifyDate})
</insert>

<insert id="addUserKey" parameterType="com.qzy.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into smbms_user values (DEFAULT,#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate},#{modifyBy},#{modifyDate})
</insert>

<delete id="delUser">
delete from smbms_user where id=#{xxx}
</delete>

<update id="modifyUser" parameterType="User">
update smbms_user set userName=#{userName} where id=#{id}
</update>

<select id="getAllUsers" resultType="User">
select * from smbms_user where userName like ‘%${value}%‘;
</select>
</mapper>
resources层
mybatis-config.(xml)
<?xml version="1.0" encoding="UTF-8" ?>

省略......
<typeAliases>
<!--单个设置别名-->
<!-- <typeAlias type="com.qzy.entity.User" alias="User"/>-->

<!--全局设置别名-->
<package name="com.qzy.entity"/>
</typeAliases>
<!--log4j配置文件的读取(可以不写)-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
省略......
</configuration>
tset层
tset(class)
private  static Logger log = Logger.getLogger(Test.class.getClass());
SqlSession sqlSession;
@Before
public void before() throws IOException {
//System.out.println("执行之前========================");
log.info("执行之前========================");
//步骤一:读取大配置文件
String source="mybatis-config.xml";
InputStream is = Resources.getResourceAsStream(source);
//步骤二:创建工厂
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
//步骤三:创建SqlSession核心对象
sqlSession = factory.openSession();
}


@Test
public void selectAllTest() throws IOException {
//步骤四:查
List<User> getAllUser = sqlSession.selectList("getAllUser");
for (User user:getAllUser) {
//System.out.println(user.getUserName());
log.info(user.getUserName());
}
}
//模糊
@Test
public void getAllUsers() throws IOException{
List<User> userList=sqlSession.selectList("com.qzy.dao.IUserDao.getAllUsers","孙");
for (User u:userList){
///System.out.println(u.getUserName());
log.info(u.getUserName());
}
}
//增
@Test
public void addUser() throws IOException{
User user =new User();
user.setUserCode("hehe");
user.setUserName("牛皮呀");
user.setUserPassword("123123");
user.setGender(2);
user.setBirthday(new Date());
user.setPhone("13215689594");
user.setAddress("北京市海淀区五道口北大青鸟");
user.setUserRole(2);
user.setCreatedBy(1);
user.setCreationDate(new Date());
user.setModifyBy(null);
user.setModifyDate(null);
sqlSession.insert("com.qzy.dao.IUserDao.addUser",user);
sqlSession.commit();
//查看是否会返回自增的主键
//System.out.println(user.getId());
log.info(user.getId());
}
//删
@Test
public void delUser() throws IOException{
sqlSession.delete("com.qzy.dao.IUserDao.delUser","17");
sqlSession.commit();
sqlSession.close();
//System.out.println("删除成功!");
log.info("删除成功!");
}
//改
@Test
public void modifyUser() throws IOException{
User user=new User();
user.setUserName("安其拉");
user.setId(16);
sqlSession.update("com.qzy.dao.IUserDao.modifyUser",user);
sqlSession.commit();
sqlSession.close();
//System.out.println("更改成功!");
log.info("更改成功!");
}
@After
public void after(){
//System.out.println("执行之后====================================");
log.info("执行之后========================");
//最后关闭sqlSession释放资源
sqlSession.close();
}

log4j.(properties)
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
###日志文件的了路径###
log4j.appender.file.File=G:\mylog.log        
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ###
###log4j.rootLogger=debug, stdout,file
log4j.logger.com.qzy.dao=debug, stdout,file

 

以上是关于idea增删改查的主要内容,如果未能解决你的问题,请参考以下文章

idea增删改查

idea使用mybatis写增删改查

idea 增删改查之 查询

idea增删改查之 新增

在 idea 下搭建的第一个MyBatis项目及增删改查用法

Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段