标签:style blog http io color ar os sp java
mysql表脚本
CREATE TABLE `item` ( `itemID` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `declaration` varchar(255) DEFAULT NULL, PRIMARY KEY (`itemID`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `order` ( `orderID` int(11) NOT NULL AUTO_INCREMENT, `usercode` varchar(225) DEFAULT NULL, `itemID` int(11) DEFAULT NULL, PRIMARY KEY (`orderID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
spring环境
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <!-- interceptor --> <bean id="loginInterceptor" class="sps.interceptor.LoginInterceptor"> <property name="loginIttService" ref="loginIttService"></property> </bean> <bean id="loginIttService" class="sps.interceptor.service.LoginIttService"> <property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property> </bean> <!-- action --> <bean id="loginAction" class="sps.login.action.LoginAction"> <property name="loginService" ref="loginService"></property> </bean> <bean id="lrcAction" class="sps.lrc.action.LrcAction"> <property name="lrcService" ref="lrcService"></property> </bean> <bean id="mybatisAction" class="sps.mybatis.action.MybatisAction"> <property name="mybatisService" ref="mybatisService"></property> </bean> <!-- service --> <bean id="loginService" class="sps.login.service.LoginService"> <property name="loginDao" ref="loginDao"></property> </bean> <bean id="lrcService" class="sps.lrc.service.LrcService"> </bean> <bean id="mybatisService" class="sps.mybatis.service.MybatisService" > <property name="mybatisDao" ref="mybatisDao"></property> </bean> <!-- dao --> <bean id="loginDao" class="sps.login.dao.LoginDao"> <property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property> </bean> <bean id="mybatisDao" class="sps.mybatis.dao.MybatisDao"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"></property> </bean> <!-- namedparameterjdbctemplate --> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource" ></constructor-arg> </bean> <!-- mybatis --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations" value="classpath:/mapper/*Mapper.xml"></property> </bean> <!-- dataSource --> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/nanoha_sps" /> <property name="username" value="root" /> <property name="password" value="nihaoya" /> </bean> </beans>
mapper
<?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="sps.mybatis.dao.MybatisDao"> <insert id="insertOrder" > insert into order(usercode, itemID) values(#{usercode},#{itemID}) </insert> <insert id="insertItem" > insert into item(name, declaration) values(#{name}, #{declaration}); </insert> <select id="queryItems" resultType="sps.mybatis.entity.Item" > select itemID, name, declaration from item </select> </mapper>
struts
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="mybatis" namespace="/mybatis" extends="spsDefault"> <!-- Add actions here <action name="HelloWorld" class="example.HelloWorld"> <result>/example/HelloWorld.jsp</result> </action> <action name="Login_*" method="{1}" class="example.Login"> <result name="input">/example/Login.jsp</result> <result type="redirectAction">Menu</result> </action> <action name="*" class="example.ExampleSupport"> <result>/example/{1}.jsp</result> </action> --> <action name="*" class="mybatisAction" method="{1}"> <result name="failure">/main/main.jsp</result> <result name="success">/mybatis/{1}.jsp</result> <result type="json" name="json">jsonMap</result> </action> </package> </struts>
action, service, dao
package sps.mybatis.action; import java.util.List; import sps.basic.content.Content; import sps.mybatis.entity.Item; import sps.mybatis.entity.Order; import sps.mybatis.service.MybatisService; public class MybatisAction { private MybatisService mybatisService; private Order order; private Item item; private List<Item> items; public String inOrder() { String result = mybatisService.insertOrder(order); return result; } public String inItem() { String result = mybatisService.insertItem(item); return result; } public String itemList() { items = mybatisService.itemList(); return Content.SUCCESS; } public void setMybatisService(MybatisService mybatisService) { this.mybatisService = mybatisService; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } public void setItems(List<Item> items) { this.items = items; } public List<Item> getItems() { return items; } public void setItem(Item item) { this.item = item; } public Item getItem() { return item; } }
package sps.mybatis.service; import java.util.List; import sps.basic.content.Content; import sps.mybatis.dao.MybatisDao; import sps.mybatis.entity.Item; import sps.mybatis.entity.Order; public class MybatisService { private MybatisDao mybatisDao; public void setMybatisDao(MybatisDao mybatisDao) { this.mybatisDao = mybatisDao; } public String insertOrder(Order order) { mybatisDao.insertOrder(order); return Content.SUCCESS; } public List<Item> itemList() { return mybatisDao.queryItems(); } public String insertItem(Item item) { mybatisDao.insertItem(item); return Content.SUCCESS; } }
package sps.mybatis.dao; import java.util.List; import org.mybatis.spring.SqlSessionTemplate; import sps.mybatis.entity.Item; import sps.mybatis.entity.Order; public class MybatisDao { private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } public void insertOrder(Order order) { } public void insertItem(Item item) { sqlSessionTemplate.insert("insertItem", item); } public List<Item> queryItems() { return sqlSessionTemplate.selectList("queryItems"); } }
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Simple jsp page</title> </head> <body> <h3>welcome, item</h3> <s:form action="mybatis/inItem.action" theme="simple"> name <s:textfield name="item.name" ></s:textfield> declaration <s:textfield name="item.declaration"></s:textfield> <s:submit value="save" ></s:submit><s:reset value="reset"></s:reset> </s:form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Simple jsp page</title> </head> <body> <h3>welcome</h3> <s:iterator value="items" var="item" > <s:property value="#item.itemID" /> <s:property value="#item.name" /> <s:property value="#item.declaration" /> </s:iterator> <s:debug></s:debug> </body> </html>
标签:style blog http io color ar os sp java
原文地址:http://www.cnblogs.com/nayyang/p/4096357.html