通过html+Servlet+MyBatis,完成站点信息的添加功能。
以下是站点表的建表语句:
CREATE TABLE `websites` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
`url` varchar(255) NOT NULL DEFAULT '',
`alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
`country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
RegisterServlet:
package com.ambow.controller;
import com.ambow.dao.UserDao;
import com.ambow.pojo.User;
import com.ambow.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/doRegister")
public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.修改编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2.获取参数
String name = request.getParameter("name");
String url = request.getParameter("url");
Integer alexa = Integer.valueOf(request.getParameter("alexa"));
String country = request.getParameter("country");
//3.构建对象
User user = new User(name, url, alexa, country);
//4.获取代理对象
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
//5.执行添加
PrintWriter out = response.getWriter();
int row = mapper.insertUser(user);
sqlSession.commit();
if (row != 0) {
out.println("添加成功!!!
");
} else {
out.println("添加失败!!!
");
}
}
}
UserDao:
package com.ambow.dao;
import com.ambow.pojo.User;
public interface UserDao {
int insertUser(User user);
}
User:
package com.ambow.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
public class User {
private int id;
private String name;
private String url;
private int alexa;
private String country;
public User(String name, String url, int alexa, String country) {
this.name = name;
this.url = url;
this.alexa = alexa;
this.country = country;
}
}
MyBatisUtil:
package com.ambow.util;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
public class MyBatisUtil {
//获取数据库链接
public static SqlSession getSqlSession(){
SqlSession sqlSession = null;
//读取主配置文件
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
//获取SqlSessionFactory - 工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession - 连接对象
sqlSession = sqlSessionFactory.openSession();
} catch (IOException e) {
e.printStackTrace();
}
return sqlSession;
}
}
UserDao.xml:
insert into websites values(null,#{name},#{url},#{alexa},#{country})
jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.2.111:3306/db1
username=root
password=Mysql666!
mybatis-config.xml:
web.xml:
register.html:
站点信息
新增站点
RegTest:
package com.ambow.test;
import com.ambow.dao.UserDao;
import com.ambow.pojo.User;
import com.ambow.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
public class RegTest {
@Test
public void test01(){
SqlSession sqlSession = MyBatisUtil.getSqlSession();
UserDao userDao = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setName("淘宝");
user.setUrl("http://taobao.com");
user.setAlexa(20);
user.setCountry("中国");
int i = userDao.insertUser(user);
sqlSession.commit();
System.out.println(i);
}
}
点击网址跳转到浏览器:
填写信息如下:
点击提交按钮:
数据库中查看信息: