SMM(spring +springmvc+mybatis)依賴註解等環境配置
pom檔案
<?xml version="1.0" encoding="UTF-8"?>
<project 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ssmproject</artifactId>
<groupId>com.lp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ssm01</artifactId>
<packaging>war</packaging>
<name>ssm01</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.9.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
</properties>
<dependencies>
<!-- spring -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log end -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
</project>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- // 掃描所有註解 -->
<context:component-scan base-package="com.lp">
<!-- // 排除 Controller 註解-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 四大資訊-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- session工廠-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- com.lp.domain.Person person-->
<property name="typeAliasesPackage" value="com.lp.domain"/>
</bean>
<!-- IPersonDao.xml IPersonDao.java-->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lp.dao"/>
<!-- <property name="beanName" value="com/lp/dao"/>-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--配置Spring框架宣告式事務管理-->
<!--配置事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!--配置AOP增強-->
<!--<aop:config>
<aop:pointcut id="service" expression="execution(* com.lp.service.impl.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>-->
<aop:config>
<aop:pointcut id="service" expression="execution(* com.lp.service.impl.*ServiceImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="service"/>
</aop:config>
</beans>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--設定配置檔案的路徑 service dao-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--解決中文亂碼的過濾器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--配置Spring的監聽器,預設只載入WEB-INF目錄下的applicationContext.xml配置檔案-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置前端控制器 controller-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--載入springmvc.xml配置檔案-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--啟動伺服器,建立該servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=ssm.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--springmvc是web層 UserController @Controller -->
<!-- 開啟元件掃描-->
<context:component-scan base-package="com.lp">
<!-- 只處理帶@Controller的請求-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置的檢視解析器物件 /WEB-INF/pages/success.jsp -->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--過濾靜態資源 .js .css png-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />
<!--開啟SpringMVC註解的支援 @RequestMapping @RequestBody @ResponseBody-->
<mvc:annotation-driven/>
</beans>
com\lp\dao\IPersonDao.xml
<?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="com.lp.dao.IPersonDao">
<select id="findAll" resultType="person">
select * from tab_person;
</select>
<insert id="save" parameterType="person">
insert into tab_person (name,money)values(#{name},#{money})
</insert>
</mapper>
com\lp\controller\PersonController.java
package com.lp.controller;
import com.lp.domain.Person;
import com.lp.service.IPersonService;
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 org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
private IPersonService personService;
@RequestMapping(path="/list",method = RequestMethod.GET)
public String list(Model model){
//顯示所有的person資料
List<Person> list = personService.findAll();
System.out.println("list() list= "+list);
//資料放在Model物件,由Model傳給頁面
model.addAttribute("list",list);//參1 key 參2 value
return "list";
}
}
IPersonDao
package com.lp.dao;
import com.lp.domain.Person;
import java.util.List;
public interface IPersonDao {
void save(Person person);
List<Person> findAll();
}
com\lp\service\impl\PersonServiceImpl.java
package com.lp.service.impl;
import com.lp.dao.IPersonDao;
import com.lp.domain.Person;
import com.lp.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PersonServiceImpl implements IPersonService {
@Autowired
private IPersonDao iPersonDao;
@Override
public List<Person> findAll() {
List<Person> list = iPersonDao.findAll(); //測試IOC時寫列印語句
return list;
}
@Override
public void savePersons(List<Person> personList) {
for (int i=0;i<personList.size();i++){
iPersonDao.save(personList.get(i));
}
}
@Override
public void savePerson(Person person) {
iPersonDao.save(person);//測試IOC時寫列印語句
}
}
IPersonService
package com.lp.service;
import com.lp.domain.Person;
import java.util.List;
public interface IPersonService {
void savePerson(Person person);
List<Person> findAll();
void savePersons(List<Person> personList);
}
com\lp\Mybatis\TestDataSource.java
package com.lp.Mybatis;
import com.lp.dao.IPersonDao;
import com.lp.domain.Person;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDataSource {
private static final Logger log = Logger.getLogger(TestDataSource.class.getName());
@Autowired
IPersonDao dao;
@Test
public void test01() throws SQLException {
List<Person> data = dao.findAll();
System.out.println(data);
}
@Test
public void test02() throws SQLException {
Person p = new Person("hello",200.00);
// log.info("dao"+dao);
// log.info("person"+p);
dao.save(p);
}
}
com\lp\Mybatis\TestMybatis.java
package com.lp.Mybatis;
import com.lp.dao.IPersonDao;
import com.lp.domain.Person;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDataSource {
private static final Logger log = Logger.getLogger(TestDataSource.class.getName());
@Autowired
IPersonDao dao;
@Test
public void test01() throws SQLException {
List<Person> data = dao.findAll();
System.out.println(data);
}
@Test
public void test02() throws SQLException {
Person p = new Person("hello",200.00);
// log.info("dao"+dao);
// log.info("person"+p);
dao.save(p);
}
}
com\lp\service\TestPersonService.java
package com.lp.service;
import com.lp.domain.Person;
import com.lp.service.impl.PersonServiceImpl;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestPersonService {
private static final Logger log = Logger.getLogger(TestPersonService.class.getName());
@Autowired
IPersonService personService;
@Test
public void test01(){//編寫兩個業務功能 查詢所有的person 儲存一個person
//建立業務物件
//IPersonService personService = new PersonServiceImpl();
//呼叫查詢
log.info(personService+"");
List<Person> list = personService.findAll();
//呼叫儲存
log.info(list+"");
Person person = new Person(1,"jack",100.00);
log.info(person+"");
personService.savePerson(person);
List<Person> list1 = personService.findAll();
log.info(list1+"");
}
@Test
public void test02(){//DI
System.out.println(personService);
}
@Test
public void test03(){
List<Person> personList = new ArrayList<>();
personList.add(new Person("jack",100.00));
personList.add(new Person("rose",200.00));
personList.add(new Person("tony",300.00));
personService.savePersons(personList);
}
}
部署圖片:
相關文章
- Spring Boot + Mybatis + Spring MVC環境配置(一) :Spring Boot初始化,依賴新增Spring BootMyBatisMVC
- Spring系列.依賴注入配置Spring依賴注入
- Java Spring各種依賴注入註解的區別JavaSpring依賴注入
- Java Web之基於註解的Spring MVC環境配置JavaWebSpringMVC
- 1.go環境安裝,IDE配置以及依賴管理GoIDE
- Spring(環境搭建&配置詳解)Spring
- 從Hessian RPC 註解方式看Spring依賴注入RPCSpring依賴注入
- 無需依賴Docker環境製作映象Docker
- spring解決迴圈依賴Spring
- 聊聊依賴注入註解@Resource和@Autowired依賴注入
- 反射、註解與依賴注入總結反射依賴注入
- iOS 搭建XMPP環境時新增依賴庫報錯及解決iOS
- 依賴管理利器---Spring IO Platform解決Spring組合依賴問題實戰SpringPlatform
- Spring依賴注入Spring依賴注入
- Spring依賴注入---Spring依賴注入
- 如何在MAVEN中配置Spring的依賴包MavenSpring
- Spring Boot 基本配置之依賴管理starter pomSpring Boot
- yum依賴python2環境-"No module named urlgrabber"Python
- Maven工程依賴配置Maven
- Android依賴注入之BufferKnife 8.0註解使用Android依賴注入
- Spring IOC——依賴注入Spring依賴注入
- Spring 依賴注入 DISpring依賴注入
- Spring Boot 起步依賴Spring Boot
- Spring迴圈依賴Spring
- spring 的依賴注入Spring依賴注入
- 程式設計新手最好別依賴整合開發環境程式設計開發環境
- 自動部署模組所需環境依賴--bat批處理BAT
- impala-shell指令碼python環境缺少依賴包指令碼Python
- 【Spring】Spring中的迴圈依賴及解決Spring
- Spring(使用註解配置)Spring
- 一文詳解spring迴圈依賴Spring
- spring 詳細講解(ioc,依賴注入,aop)Spring依賴注入
- 反射,註解,動態代理,依賴注入控制反轉反射依賴注入
- spring下應用@Resource, @Autowired 和 @Inject註解進行依賴注入的差異Spring依賴注入
- Linux環境安裝Oracle11g(一)——配置檢查及依賴項安裝LinuxOracle
- 【Spring系列】- Spring迴圈依賴Spring
- Spring Boot - Profile不同環境配置Spring Boot
- Spring.Net 依賴注入Spring依賴注入