spring+springmvc+mybatis+mysql实现登录功能(上)

注:classpath 指的为target文件夹,classpath*为有多个classpath时使用。


1.在idea中建立maven 工程。

具体过程:File -》new project -》选择maven  勾选create from archetype,选中以webapp结尾的一行,点击next,groupid以及artifactid可以随意起名,点击next,选择加号,添加key:archetypeCatalog,value:internal,点击next,之后就随意起名,放在d盘,最好不是c盘下。next,完成后就出现web工程。

2.建立目录结构(注意如果建立的有误,则不能正确读取到文件,一个颜色为一组结构,同属于main下)

层级为:src-》main->(java->com->yy->(controller,,dao->(IUserDao.java),,model->(User.java),,service),,,,resources->(config->(applicationContext.xml,,config.xml,,jdbc.properties),,,mapper->(IUserDao.xml)),,test-》Test.java...,,webapp->**.jsp,与(WEB-INF->(web.xml,log4j.xml)),(target为自动生成的),pom.xml

3.controller为控制层,主要用于对业务模块的流程控制。 
dao为数据接入层,主要用于与数据库进行连接,访问数据库进行操作,这里定义了各种操作数据库的接口。 
mapper中存放mybatis的数据库映射配置。可以通过查看mybatis相关教程了解 
model中存放了我们的实体类 
service为业务层,我们的各种业务都定义在此,由controller调用不同业务实现不同的操作。

4.具体各部分程序

(1)pom.xml

xml version="1.0" encoding="UTF-8"?>

xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  war
  spring
  com.yy
  spring
  1.0-SNAPSHOT
  
    UTF-8
    4.1.4.RELEASE
    2.5.0
  

  
  
    junit
    junit
    4.12
    test
  
    
    
      org.springframework
      spring-core
      ${spring.version}
    

    
      org.springframework
      spring-beans
      ${spring.version}
    

    
      org.springframework
      spring-context
      ${spring.version}
    

    
      org.springframework
      spring-tx
      ${spring.version}
    

    
      org.springframework
      spring-web
      ${spring.version}
    

    
      org.springframework
      spring-webmvc
      ${spring.version}
    

    
      org.springframework
      spring-jdbc
      ${spring.version}
    

    
      org.springframework
      spring-test
      ${spring.version}
      test
    

    
    
      org.mybatis
      mybatis
      3.2.8
    

    
    
      org.mybatis
      mybatis-spring
      1.2.2
    

    
    
      mysql
      mysql-connector-java
      5.1.34
    

    
    
      com.mchange
      c3p0
      0.9.5-pre8
    

    
      org.aspectj
      aspectjweaver
      1.8.4
    
    
    
      log4j
      log4j
      1.2.17
    

    
    
      javax.servlet
      servlet-api
      3.0-alpha-1
    
    
      javax.servlet
      jstl
      1.2
    
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.13
    
  


  
    
      
        org.mortbay.jetty
        maven-jetty-plugin
        6.1.7
        
          
            implementation="org.mortbay.jetty.nio.SelectChannelConnector">
              8888
              30000
            
          
          ${project.build.directory}/${pom.artifactId}-${pom.version}
          /
        
      
    
  

(2)web.xml具体代码
xml version="1.0" encoding="UTF-8"?>

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">
  
    
     
        contextConfigLocation
        
           classpath*:config/applicationContext.xml
        
     
  
  
      
        org.springframework.web.context.ContextLoaderListener
      
  
    
       encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
          UTF-8
          
     
           forceEncoding
            true
       
     
    
          encodingFilter
        /*
   

        

(3)配置完web.xml后,配置spring的applicationContext.xml,它是spring的配置文件,一般与spring集成的框架都要在这里进行配置。

xml version="1.0" encoding="UTF-8"?>



xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:property-placeholder location="classpath*:config/jdbc.properties"/>
    
    <context:component-scan base-package="com.yy.service"/>
    
    id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        name="driverClass" value="${jdbc_driverClassName}"/>
        name="jdbcUrl" value="${jdbc_url}"/>
        name="user" value="${jdbc_username}"/>
        name="password" value="${jdbc_password}"/>
        
        name="maxPoolSize" value="20"/>
        
        name="minPoolSize" value="2"/>
        
        name="initialPoolSize" value="2"/>
        
        name="maxConnectionAge" value="6000"/>
        
        name="maxIdleTime" value="60"/>
    
    
    id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        name="dataSource" ref="dataSource"/>
        
        name="configLocation" value="classpath:config/config.xml"/>
        
        name="mapperLocations" value="classpath*:mapper/*.xml"/>
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        name="basePackage" value="com.yy.dao"/>
    
    
    id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        name="dataSource" ref="dataSource"/>
    
()
(4)

在这里使用了jdbc.properties来分散配置,jdbc.properties中保存了数据库的信息,需要根据你们的数据库配置进行修改。

jdbc.properties

jdbc_driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/demo95?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=123456
(5)

然后是争对mybatis进行配置,由于我把数据库配置都配置在applicationcontext.xml中,所以我对mybatis中只进行了别名配置。

config.xml

xml version="1.0" encoding="UTF-8" ?>
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

    
    
        alias="User" type="com.yy.model.User"/>
    

(6)

创建完数据库后开始写model下面的实体类

User.java

package com.yy.model;

/**
 * Created by yy on 2017/9/6.
 */
public class User {
    private int id;
    private String username;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = 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;
    }
}

(7)

数据库接入层dao的接口,在这里我使用了mybatis以接口方式编程,这部分借鉴了其他的教程,有不懂的地方可以查看 http://www.yihaomen.com/article/java/302.htm  该网址的mybatis教程,我觉得写的很不错。

package com.yy.dao;
import com.yy.model.User;
/**
 * Created by yy on 2017/9/6.
 */
public interface IUserDao {
    public User selectById(int id);
    public User selectByName(String name);
}
(8) mybatis映射文件文件名必须与接口类相同,否则无法映射成功。

xml version="1.0" encoding="UTF-8" ?>
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


namespace="com.yy.dao.IUserDao">
    
    

    



(9)好了,到这里mybatis与spring已经整合完毕,我们需要测试一下mybatis是否与spring整合成功,写一个test类

import com.yy.dao.IUserDao;
import com.yy.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * Created by yy on 2017/9/6.
 */
public class Test {
    private static ApplicationContext ac;
    static {
        ac = new ClassPathXmlApplicationContext("config/applicationContext.xml");
    }
    public static void main(String[] args) {
        IUserDao mapper = (IUserDao) ac.getBean("IUserDao");
        System.out.println("获取alvin");
        User user = mapper.selectByName("yy");
        System.out.println(user.getId()+":"+"username:"+user.getUsername());
        System.out.println("password:"+user.getPassword());
    }

}
如果成功,如下图所示:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
获取alvin
1:username:yy
password:123 

你可能感兴趣的:(spring,mybatis学习记录)