用java,html和mybatis写一段从文本框获取id,再通过按钮查询数据库信息最后返回到页面显示
时间: 2024-05-07 13:21:12 浏览: 76
好的,以下是一个简单的示例代码:
HTML页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>查询用户信息</title>
</head>
<body>
<form>
用户ID:<input type="text" id="userId"><br>
<button type="button" onclick="getUserInfo()">查询</button>
</form>
<p id="userInfo"></p>
</body>
<script>
function getUserInfo() {
var userId = document.getElementById('userId').value;
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('userInfo').innerText = xhr.responseText;
}
};
xhr.open('GET', '/getUserInfo?id=' + userId, true);
xhr.send();
}
</script>
</html>
```
Java Servlet:
```java
@WebServlet("/getUserInfo")
public class GetUserInfoServlet extends HttpServlet {
private UserDao userDao;
@Override
public void init() throws ServletException {
userDao = new UserDaoImpl();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id = req.getParameter("id");
User user = userDao.getUserById(id);
if (user != null) {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write("用户ID:" + user.getId() + "\n用户名:" + user.getName() + "\n年龄:" + user.getAge());
} else {
resp.sendError(404, "用户不存在");
}
}
}
```
UserDao:
```java
public interface UserDao {
User getUserById(String id);
}
```
UserDaoImpl:
```java
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl() {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Override
public User getUserById(String id) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
return sqlSession.selectOne("getUserById", id);
}
}
}
```
mybatis-config.xml:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias type="com.example.User" alias="User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/UserMapper.xml"/>
</mappers>
</configuration>
```
UserMapper.xml:
```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">
<mapper namespace="com.example.UserDao">
<select id="getUserById" parameterType="String" resultType="User">
SELECT id, name, age FROM user WHERE id = #{id}
</select>
</mapper>
```
以上代码仅供参考,实际开发中需要根据具体情况进行修改。
阅读全文