mybatis+servlet
作者:互联网
实现两个功能-增加信息、信息查询
结构目录
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wxy</groupId> <artifactId>annotation</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.16</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.glassfish.web</groupId> <artifactId>javax.servlet.jsp.jstl</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency> </dependencies> </project>
userdao.java
package com.wxy.dao; import com.wxy.domain.user; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import java.util.List; public interface userdao { //用户的持久层接口 @Insert("insert into emp(empid,first_name,salary) values(#{empid},#{first_name},#{salary})") void saveuser(user u); @Select("select * from emp where salary>500") List<user> findall(); }
user.java
package com.wxy.domain; import java.io.Serializable; public class user implements Serializable { private Integer empid; private String first_name; private Integer salary; //public static List<user> a; public void setEmpid(Integer empid) { this.empid = empid; } public void setFirst_name(String first_name) { this.first_name = first_name; } public void setSalary(Integer salary) { this.salary = salary; } public Integer getEmpid() { return empid; } public String getFirst_name() { return first_name; } public Integer getSalary() { return salary; } @Override public String toString() { return "user{" + "empid=" + empid + ", first_name=" + first_name + ", salary=" + salary + "}\n"; } }
addservlet.java
package com.wxy.servlet; import com.wxy.dao.userdao; import com.wxy.domain.user; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URL; import java.util.List; public class addservlet extends HttpServlet { private static final long serialVersionUID=1L; public void doGet(HttpServletRequest request,HttpServletResponse resp) throws FileNotFoundException { String resource="./mybatis-config.xml"; URL o=Thread.currentThread().getContextClassLoader().getResource(""); String path=o.getPath().replace("classes","classes"); InputStream in; in=new FileInputStream(new File(path+resource)); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in); SqlSession session=sqlSessionFactory.openSession(); userdao userdao=(userdao)session.getMapper(userdao.class); user a = new user(); a.setEmpid(Integer.parseInt(request.getParameter("empid"))); a.setFirst_name(request.getParameter("first_name")); a.setSalary(Integer.parseInt(request.getParameter("salary"))); userdao.saveuser(a); List<user> list=userdao.findall(); System.out.println(list.toString()); session.close(); } }
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mtbatis.org/dtd/mybatis-3-config.dtd"> <!-- mybatis的主配置文件 --> <configuration> <!-- 配置环境 --> <environments default="mysql"> <!-- 配置MySQL的环境 --> <environment id="mysql"> <transactionManager type="JDBC"></transactionManager> <!-- 配置数据源(连接池 --> <dataSource type="POOLED"> <!-- 配置联接数据库的四个基本信息 --> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false"/> <property name="username" value="root"/> <property name="password" value="2999029aaa"/> </dataSource> </environment> </environments> <!-- 指定映射配置文件的位置,映射文件指的是每个dao独立时配置文件 --> <mappers> <mapper class="com.wxy.dao.userdao"/> <!-- <mapper resource="usersMaooing.xml"/> --> </mappers> </configuration>
add.xml
<%@ page contentType="text/html;charset=UTF-8" language="java" import="com.wxy.domain.user" %> <html> <head> <body> <form action="addd" method="get"> empid:<input type="text" name="empid" value=""/><br/> first_name:<input type="text" name="first_name" value=""><br/> salary:<input type="text" name="salary"value=""><br/> <input type="submit" value="增加"/> </form> </body> </head> </html>
测试结果:
标签:salary,empid,name,mybatis,import,servlet,public 来源: https://www.cnblogs.com/zuiaimiusi/p/11921572.html