废话不说直接上思路:
1.先用maven安装mybatis
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
2.导入mybatis相关驱动
<!--导入依赖-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
3.编写mybatis工具类,目得简化编写难度
import java.io.IOException;
public class mybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取SqlSession连接
public static SqlSession getSession() {
return sqlSessionFactory.openSession();
}
}
4.在“resources”文件下编写 "mybatis-config.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>
<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/mybatis"/>//连接数据库的目录
<property name="username" value="root"/>//数据库名字,默认root
<property name="password" value="123456"/>//数据库密码,自己的数据库密码,一般为root
</dataSource>
</environment>
</environments>
</configuration>
4.创建mapper文件,在mapper文件下创建"xxxMapper.xml"(这个文件可以理解为"Dao层的mysql语句存放处")
<?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="mapper.userMapper">
<select id="selectFromDao" parameterType="map" resultType="com.custmor.customer">
select * from customer where owner_username=#{username} and state =#{state}
</select>
<select id="selectfromId" parameterType="map" resultType="com.custmor.user">
select * from users where username=#{username} and password=#{password}
</select>
</mapper>
mapper namespace = “Mapper.xml存放位置,具体到每个目录”
select id =“对应mapper接口中方法名称”
paramterType = “参数类型”
resultType=“mysql对应数据类的具体位置,具体到每个目录”
5.配置完毕后,可以创建xxxMapper接口,方法名要和Mapper.xml.id一致; 先完成五步再去配置第四步,我认为是最优的选择;
ps:可以对比第四步,看看配置是否一致;
package mapper;
import java.util.List;
public interface userMapper {
java.util.List<customer> selectFromDao(java.util.Map<String, Object> map);
List<user> selectfromId(java.util.Map<String, Object> maps);
}
6.创建Mapper接口的implement实现方法,
(1)创建session工厂:调用mybatis工具类,getsession()即可创建,创建后赋值一个空间;因为任何事务都需要落脚点才能继续运行;所以要赋值给Sqlsession session;
(2)创建工厂后,要用工厂读取Mapper.xml,SQL语句,然后赋值给userMapper接口里;
(3)最后,使用userMapper接口,调用里面的方法;然后赋值给一个空间,然后读取空间的数据;
(4)把session工厂给关闭. 语法为session.close();
public class userMapperService implements userMapper {
public List<customer> selectFromDao(Map<String, Object> map) {
SqlSession session = utils.mybatisUtils.getSession();
userMapper mapper = session.getMapper(userMapper.class);
List<customer> user = mapper.selectFromDao(map);
for (customer cus : user) {
System.out.println(cus);
}
session.close();
return user;
}
}
7. 创建contorller层,生成一个接口传给前端调用;
(1)extends "HttpServlet" 创建其中的doget方法;
(2)设置Http请求的编码方式 需要用参数req中的 setCharacterEncoding设置;
(3)设置Http请求投,需要用参数req中的setContenType设置;
(4)获取前端用户输入的值,需要用参数req中的getParameter获取;
(5)获取完毕之后,创建接口对象,把Mapper接口的implement实现方法赋值给接口;
(6)调用对象中的方法,把获取的值传入方法参数中;
(7)创建ObjectMapper json格式,调用objectMapper中的转换方法然后把以上的接口名称传递过去即可转换,最后创建一个相应的接收变量,然后把objectMapper赋值进去;
(8)然后用resp.getwriter().write(“this.变量”) 即可完毕;
@WebServlet("/select")
public class testFromMap extends HttpServlet {
@Override
protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException, java.io.IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
String name = req.getParameter("owner_username");
String state = req.getParameter("state");
// String names = req.getParameter("name");
java.util.Map<String, Object> maps = new HashMap<String, Object>();
maps.put("username", name);
maps.put("state", state);
// maps.put("name", names);
userMapper mapper = new userMapperService();
List<customer> list = mapper.selectFromDao(maps);
ObjectMapper map = new ObjectMapper();
String object = map.writerWithDefaultPrettyPrinter().writeValueAsString(list);
resp.getWriter().write(object);
}
}
标签:mapper,java,入门,创建,req,接口,mybatis,写法 From: https://www.cnblogs.com/ZhuAo/p/16889311.html