方式同上 ⬆
二. 在idea中构建maven web项目并整合sm
1 引入相关依赖
<!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--struts2-core依赖--> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16</version> </dependency> <!--引入mybatis核心依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!--引入mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--引入jstl--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
2.mybatis相关开发 entity dao service
1>.书写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="mysql"> <!--定义oracle开发环境--> <environment id="oracle"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.oracle.jdbc.Driver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" /> <property name="username" value="hr" /> <property name="password" value="hr" /> </dataSource> </environment> <!--定义mysql环境--> <environment id="mysql"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/2001" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> </configuration>
2.开发MybatisUtils
/** * mybatis 工具类 */ public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> t = new ThreadLocal<>(); static{ InputStream is = null; try { is = Resources.getResourceAsStream("mybatis-config.xml"); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); }finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 返回sqlsession的方法 * @return */ public static SqlSession getSqlSession(){ SqlSession sqlSession = t.get(); if(sqlSession==null){ sqlSession = sqlSessionFactory.openSession(); t.set(sqlSession); } return sqlSession; } /** * 关闭sqlSession */ public static void closeSqlSession(){ SqlSession sqlSession = t.get(); if(sqlSession!=null){ sqlSession.close(); t.remove(); } } }
3.测试工具类是否正确
public class TestMybatisUtils { public static void main(String[] args) { SqlSession sqlSession = MybatisUtils.getSqlSession(); System.out.println("sqlSession = " + sqlSession); } }
4.建表
create table t_user( id varchar(40) primary key, name varchar(60), age int(4), bir date );
5.开发实体类 entity
public class User { private String id; private String name; private Integer age; private Date bir; //alt + insert public User(String id, String name, Integer age, Date bir) { this.id = id; this.name = name; this.age = age; this.bir = bir; } public User() { } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBir() { return bir; } public void setBir(Date bir) { this.bir = bir; } @Override public String toString() { return "User{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age=" + age + ", bir=" + bir + '}'; } }
6.开发DAO接口
//创建的DAO接口 public interface UserDAO { //查询所有 List<User> findAll(); }
7.开发Mapper配置文件
- 在resources目录中创建目录一定要使用"" 形式进行目录分割
<!--namespace 对应哪个DAO接口--> <mapper namespace="com.baizhi.dao.UserDAO"> <!--查询所有--> <select id="findAll" resultType="com.baizhi.entity.User"> select id,name,age,bir from t_user </select> </mapper>
8.注册mapper配置文件
<?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="mysql">.....</environments> <!--注册mapper配置文件--> <mappers> <mapper resource="com/baizhi/mapper/UserDAOMapper.xml"/> </mappers> </configuration>
9.测试DAO
public class TestUserDAO { public static void main(String[] args) { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDAO userDAO = sqlSession.getMapper(UserDAO.class); //foreach遍历 /*List<User> users = userDAO.findAll(); for (User user : users) { System.out.println(user); }*/ //lambada userDAO.findAll().forEach(user-> System.out.println("user = " + user)); } }
10.开发service层
1.接口开发
public interface UserService { List<User> findAll(); }
2.实现类开发
public class UserServiceImpl implements UserService { @Override public List<User> findAll() { SqlSession sqlSession = MybatisUtils.getSqlSession(); UserDAO userDAO = sqlSession.getMapper(UserDAO.class); List<User> users = null; try{ users = userDAO.findAll(); }finally { MybatisUtils.closeSqlSession(); } return users; } }
3.测试业务层
public class TestUserService { public static void main(String[] args) { UserServiceImpl userService = new UserServiceImpl(); userService.findAll().forEach(user -> System.out.println("user = " + user)); } }
5.strtus2相关开发 controller
1.配置web.xml核心filter
<!--配置struts2核心filter--> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2.引入struts.xml配置文件
- 注意: struts.xml 必须在项目中src/main/resources根目录中 名字必须为 struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> </struts>
3.开发控制器
public class UserAction { private UserService userService = new UserServiceImpl(); private List<User> users;//成员变量用来传递数据 //控制器方法用来完成用户查询所有功能 public String findAll(){ //1.收集数据 //2.调用业务方法 this.users = userService.findAll(); //3.流程跳转 return "success"; } //get set方法 public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } }
4.配置Action
<struts> <!--包--> <package name="user" extends="struts-default" namespace="/user"> <!--action--> <action name="findAll" class="com.libin.action.UserAction" method="findAll"> <result name="success">/showAll.jsp</result> </action> </package> </struts>
6.jsp页面相关开发
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>展示用户数据</title> </head> <body> <h1>遍历用户数据</h1> <c:forEach items="${requestScope.users}" var="user"> ${user.id} -----${user.name}-----${user.age}-----${user.bir} <br> </c:forEach> </body> </html>
7.部署项目运行
- 访问: http://localhost:端口号/sm/user/findAll