servlet+mybatis实现crud(增删改查)
⽬的:使⽤servlet+mybatis+Mysql完成对学⽣信息表的增删改查操作,并结合jsp展⽰效果项⽬结构
准备⼯作>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1.建表
附上建表代码:
附上SQL代码:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`name` varchar(150) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
2.所需jar包
引⼊mybatis
1.将mybatis-3.4.1.jar,mysql-connector-java-5.1.38.jar放到lib⽬录下,并buildPath。
2.⿏标右键点击项⽬名,新建⼀个名为resource的资源⽂件,注意是这种图标
资源⽂件放到这⾥才能被识别。在resource⽂件夹下新建⼀个db.properties⽂件
放连接数据库信息
db.properties
sql.jdbc.Driver
url=jdbc:mysql:///student?characterEncoding=UTF8&useSSL=true&serverTimezone=UTC username=root
password=root
再在该⽬录下建⼀个名为l的xml配置⽂件
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<!-- <typeAliases>
<typeAlias alias="" type="" />
</typeAliases> 这⾥是配置别名-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="" />
</mappers>
</configuration>mysql面试题 增删改查
src下创建domain包存放实体类,domain下新建⼀个名为Student的java类Student.java
/**
* 学⽣实体类
* @author 红烛
字符串长度函数c++*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private Integer age;
private Date birthday;
private String address;
private String phone;
private String email;
}
src下创建mapper包,新建StudentMapper接⼝和l⽂件StudentMapper接⼝中什么都不⽤写,我们把接⼝写在dao层
StudentMapper.java
/**
*
* @author 红烛
*
*/
public interface StudentMapper {
}
<!DOCTYPE mapperpython爬虫开发
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.wolfcode.mapper.StudentMapper">
<sql id="Base_Column_List">
id,name,age,birthday,address,phone,email
</sql>
python gui编程<!-- 添加查询 -->
<insert id="insert">
insert into student(id,name,age,birthday,address,phone,email)values(#{id},#{name},#{age},#{birthday},#{address},#{phone},#{email}) </insert>
<!-- 删除查询 -->
<delete id="delete">
delete from student where id=#{id}
</delete>
<!-- 修改查询 -->
<update id="update" parameterType="cn.wolfcode.domain.Student">
update student
<set>
<if test="name!=null">name=#{name},</if>
<if test="age!=null">age=#{age},</if>
<if test="birthday">birthday=#{birthday},</if>
c语言编译器中文<if test="address!=null">address=#{address},</if>
<if test="phone!=null">phone=#{phone},</if>
<if test="email!=null">email=#{email},</if>
</set>
where id=#{id}
</update>
<!-- 查询某个学⽣ -->
<select id="select" resultType="cn.wolfcode.domain.Student">
select
<include refid="Base_Column_List"></include>
from student where id=#{id}
</select>
<!-- 查询所有学⽣ -->
<select id="selectAll" resultType="cn.wolfcode.domain.Student">
select
<include refid="Base_Column_List"></include>
from student
</select>
</mapper>
在src下创建dao.impl包,在dao包下创建IStudentDAO接⼝,在dao.impl下创建StudentDAOImpl实现类。IStudentDAO.java
/**
*
* @author 红烛
*
*/
public interface IStudentDAO {
/**
* 添加学⽣信息
* @param stu
zipentry文件名乱码*/
void insert(Student stu);
/**
* 删除学⽣信息
* @param id
*/
void delete(Long id);
/**
* 修改学⽣信息
* @param stu
*/
void update(Student stu);
/**
* 查询某个学⽣信息
* @param id
* @return
*/
Student select(Long id);
/**
* 查询所有学⽣信息
* @return
*/
List<Student> selectAll();
}
StudentDAOImpl.java
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论