1,创建项目;
2,建立数据库;
3,用使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件;
1,修改配置文件generator.xml
<classPathEntry location="D:\mysql-connector-java-5.1.34.jar" /> 配置文件中需要把jar包放入指定的路径 <table schema="test" tableName="easybuy_news" domainObjectName="News" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="true" /> </table> 配置文件创建实体类的时候需要设置驼峰命名规则2,创建MybatisGeneratorUtil类
public class MybatisGeneratorUtil { /** * @param args */ public static void main(String[] args) { try { System.out.println("start generator ..."); List<String> warnings = new ArrayList<String>(); boolean overwrite = true; File configFile = new File(MybatisGeneratorUtil.class.getResource("/generator.xml").getFile()); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(configFile); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); System.out.println("end generator!"); } catch (IOException e) { e.printStackTrace(); } catch (XMLParserException e) { e.printStackTrace(); } catch (InvalidConfigurationException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
3,运行MybatisGeneratorUtil类
(我运行时该类时,报了一个配置文件没找到的错,经过排查,整个项目更换了一个无中文无空格的路径,我也不知道原因)
4,类都已经创建好了,但是Mapper.java都是
<?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 type="cn.jbit.pojo.User" alias="User" /> --> <package name="com.jbit.pojo"/> </typeAliases> </configuration>
5,修改Mapper.xml文件(里面的方法都是不太适合自己用的)
6,往项目中整合入spring
1,编写 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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 使用jndi数据源作为datasource --> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/easybuy"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="mapperLocations" value="classpath:com/jbit/dao/*.xml"></property> </bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.jbit.dao" ></property> </bean><!-- 使用注解的方式注入,实现解耦合的效果 --> <context:component-scan base-package="com.jbit.service"></context:component-scan> <!-- 配置事物 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="login" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <!-- <tx:method name="*" propagation="REQUIRED"/> --> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(public * com.jbit.service.impl..*(..))" id="serviceMethod" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config> </beans>
7,编写service和serviceImpl层,并在serviceimpl类里写注解,即@service("userService")和@autowired
8,开始整合springMVC:
1,在applicationContext.xml中配置
<context:component-scan base-package="com.jbit.controller"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> 2,修改web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3,编写controller类;
package com.jbit.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.jbit.pojo.User; import com.jbit.service.UserService; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping("/list") public String login(Model model){ List<User> userList=userService.login(); model.addAttribute("userList", userList); return "list"; } } (简易的测试用类,本小白没想到好办法,将就一下,还烦请各位大佬能给点好的建议)
4,编写/jsp/list.jsp页面测试(此页面啥都没写)
到这里基本就是其他的小内容了,完结撒花
所需jar包:
1,步骤3中所需jar包:mybatis-generator-core-1.3.2.jar
mysql-connector-java-5.1.34.jar
所需的配置文件:generator.xml
需要链接数据库,所以要数据库的属性文件:jdbc.properties
2,Mybatis的包:mybatis-3.2.4.jar
3,log4j的包:log4j-1.2.17.jar
4,spring所需jar包:spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-tx-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-jdbc-3.2.13.RELEASE.jar
5,springMVC所需jar包:aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
commons-logging-1.1.1.jar
spring-web-3.2.13.RELEASE.jar
spring-webmvc-3.2.13.RELEASE.jar
6,jsp页面需要的jar包:jstl.jar和standard.jar
fastjson包:fastjson-1.2.2.jar
7,还有好多jar包不知道啥用,以后再补全