java ssm框架入门(一)面向接口编程

因为工作上用到spring + strtus2 + mybatis ,所以开始学习下这个框架。

这里用到的是MySQL数据库

java ssm框架入门(一)面向接口编程

首先从web.xml 开始

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <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> </web-app>

这里引用了struts 和spring的配置文件

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<package name="myPackage" extends="struts-default" >
<action name="login" class="com.test.action.LoginAction"> <result name="success">/success.jsp</result>
<result name="error">/errer.jsp</result>
</action>
</package>
</struts>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!--配置数据源属性文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/configs/sqlServer.properties</value>
</property>
</bean> <!--配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.pwd}</value>
</property>
</bean>
<!-- 配置sessoin工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis.xml" />
<!-- <mappers>
<mapper resource="com/test/sqlMapper/loginMapper.xml"/>
</mappers> 这里动态的加载 xml文件,就不用手动在 :mybatis.xml 里面写了这里加载的时候是作为session工厂一起进行加载-->
<property name="mapperLocations" value="classpath:com/test/sqlMapper/*Mapper.xml" />
<property name="dataSource" ref="dataSource" />
</bean> <bean id="loginDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 用上面直接指定接口位置-->
<property name="mapperInterface" value="com.test.dao.ILoginDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> <!-- <bean id="loginAction" class="com.test.action.LoginAction">
<property name="loginDao" ref="loginDao"></property>
</bean> -->
</beans>

sql的配置文件

WEB-INF/configs/sqlServer.properties

jdbc.url=jdbc:mysql://localhost:3306/login
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.pwd=1234

mybatis.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 alias="userinfo" type="com.test.entity.UserInfo"/>
</typeAliases>
<!-- <mappers>
<mapper resource="com/test/sqlMapper/loginMapper.xml"/>
</mappers> -->
</configuration>

mybatis.xml 包含的文件

Ssm\src\com\test\sqlMapper\UserInfoMapper.xml

使用接口com.test.dao.ILoginDao来管理 Dao里面的方法

<?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">
<!-- 这个namespace可以理解是调用sql方法的一个执行类 -->
<mapper namespace="com.test.dao.ILoginDao">
<!-- 这里是完成映射。type里面是映射的实体 column里面是这个实体里面和property表字段想对应的属性 -->
<resultMap type="com.test.entity.UserInfo" id="userMap">
<id property="id" column="id" jdbcType="DECIMAL"/>
<result property="username" column="username" jdbcType="VARCHAR" />
<result property="password" column="password" jdbcType="VARCHAR" />
</resultMap> <select id="getUser" parameterType="String" resultMap="userMap">
select * from userinfo where username=#{userName}
</select> <select id="getUserName" parameterType="int" resultMap="userMap">
select * from userinfo where id=#{id}
</select> </mapper>

mybatis映射文件对应的实体类(类和数据库名一致最好)

package com.test.entity;

public class UserInfo {

    private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

通过配置到bean里面的接口去实现sql方法。

因为spring 里面的bean就是给类的进行实例化的。然后通过id进行调用。

mybatis就是通过id进行调用所属的sql的。

那就直接通过spring去实例化一个类(因为不用写类的具体实现,所以就用接口就行),然后去管理mybatis里面的得到id的方法。

Ssm\src\com\test\dao\ILoginDao.java

package com.test.dao;

import java.util.List;

public interface ILoginDao {
public List getUser(String userName);
public List getUserName(int userID);
}

最后是一个action的内容

Ssm\src\com\test\action\LoginAction.java

package com.test.action;

import java.util.List;

import com.test.dao.ILoginDao;

public class LoginAction {

        private ILoginDao loginDao;
private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} public ILoginDao getLoginDao() {
return loginDao;
}
public void setLoginDao(ILoginDao loginDao) {
this.loginDao = loginDao;
} public String execute(){
String userName = getUsername();
String password = getPassword();
System.out.println("userName:"+userName+"\n"+"password:"+password);
List list = loginDao.getUser(userName);
List list2 = loginDao.getUserName(1);
if(list.size()>0){
return "success";
}else{
return "error";
} } }

java ssm框架入门(一)面向接口编程

访问路径:http://localhost:8080/Ssm/login.jsp

java ssm框架入门(一)面向接口编程

源码:http://pan.baidu.com/s/1i43PH3Z

上一篇:集成学习之Boosting —— Gradient Boosting原理


下一篇:C++转换unicode utf-8 gb2312编码