SSM框架——源服務器未能找到目標資源的表示或者是不願公開一個已經存在的資源表示。

我在這先說出我的例子
邏輯的開始是一個簡單的jsp文件
一個from標籤鏈接的是search文件,method屬性是post代碼如下

<body>
	<form action="search" method="post">
		姓名:<input name="name"/> 學號<input name="sno"/> 性別:<input nmae="gender"/>
		<input type="submit" value="搜索"/>
	</form>
</body>

之後根據網頁的配置文件web.xml中查詢search文件並點擊搜索按鈕的時候將數據傳給該文件,並調用該servlet的post方法;

 <!--servletContext監聽器-->
  <listener>
  	<listener-class>jee.pk3.AppListener</listener-class>
  </listener>
  
  <servlet>
  	<servlet-name>SearchServlet</servlet-name>
  	<display-name>SearchServlet</display-name>
  	<servlet-class>jee.pk3.SearchServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>SearchServlet</servlet-name>
  	<url-pattern>/SearchServlet</url-pattern>
  </servlet-mapping>

上面代碼中有一個servleContextLIstener監聽器,該監聽器的主要功能是當servletcontext容器調用時調用SqlSessionFactoryUtil.init();也就是當search文件的servletcontext容器調用的時候監聽器就會調用SqlSessionFactoryUtil.init();

public class AppListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
	   
		try {
			SqlSessionFactoryUtil.init();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

}

下面我們看看servlet文件中的代碼。聯繫上面jsp文件中的代碼以及web.xml代碼可以看出search指的就是searchServlet文件,jsp文件傳值也會傳到該servlet文件的dopost方法的req參數中。然後借用req就可以調用jsp文件中的值了。
在下面就是mybatis的代碼了,首先是實例化SqlSessionFactory ,該實例化涉及到剛纔的配監聽器,在調用servlet文件的時候servletcontext容器就會被調用,監聽器的contextInitialized方法就會被調用,從而調用SqlSessionFactoryUtil.init();

public class SearchServlet extends HttpServlet {
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String name=req.getParameter("name");
		String sno=req.getParameter("sno");
		String gender=req.getParameter("gender");
	    Student stu=new Student();
	    stu.setName(name);
	    stu.setSno(sno);
	    stu.setGender(gender);

		SqlSessionFactory sqlSessionFactory=SqlSessionFactoryUtil.getInsertance();
		try (SqlSession sqlSession=sqlSessionFactory.openSession(true)){
			StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
			List<Student> list=studentMapper.search(stu);
			System.out.println(list);
		} 
	}

}

下面是mybatis查詢數據庫的sqlSessionFactory 的實例化,也就是在servlet文件被調用的時候,該實例化就已經完成。這樣書寫的目的是保證sqlSessionFactory的單例,避免多次實例化sqlSessionFactory造成資源的浪費。

public class SqlSessionFactoryUtil {

	public static SqlSessionFactory sqlSessionFactory;
	public static void init() throws IOException {
		String config="mybatis_config.xml";
		InputStream is=Resources.getResourceAsStream(config);
		sqlSessionFactory =new SqlSessionFactoryBuilder().build(is);
	}
	
	public static SqlSessionFactory getInsertance() {
		return sqlSessionFactory;
	}
	
}

數據庫的鏈接說完了,接下來我們說一說搜索語句的配置。首先我們要一個搜索方法的接口。接口代碼如下:

public interface StudentMapper {
	public List<Student> search(Student stu);
}

下面是sql搜索的配置文件,首先使用mapper標籤確定映射的接口;然後使用resultMap標籤根據實體類將數據庫中的字段映射到實體對應的屬性上。最後調用select標籤將接口中的方法設爲id,parameterType屬性設置參數方法的參數,resultMap屬性設置的是映射關係。select內部是sql語句。

<?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="jee.pk3.StudentMapper">
<resultMap type="jee.pk3.Student" id="studentResultMap">
	<id property="id" column="S_id" ></id>
	<result property="name" column="s_name"/>
	<result property="sno" column="s_no"/>
	<result property="gender" column="s_gender"/>
	
</resultMap>
<select id="search" parameterType="jee.pk3.Student" resultMap="studentResultMap">
	select * from t_students where s_name=#{name}
</select>
</mapper>

好了我的分享到此結束,如果你的所有邏輯都能通順該錯誤就不會出現,所以請你仔細檢查自己的代碼是否出現各個傳遞層次的斷層。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章