• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解MyEclipse中搭建spring-boot+mybatis+freemarker框架

java 搞代码 4年前 (2022-01-05) 35次浏览 已收录 0个评论

这篇文章主要介绍了详解MyEclipse中搭建spring-boot+mybatis+freemarker框架,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1.在MyEclipse里创建一个maven项目。File>New>Maven Project:

勾选图中红色部分,然后点击Next。

2.填写下图中红色部分然后点击Finish。

3.此时一个maven项目已经生成,目录结构如下:

4.打开pom.xml在里面编辑如下内容:

  4.0.0com.lm.spring-bootspring-boot0.0.1-SNAPSHOT UTF-8 org.springframework.bootspring-boot-starter-parent1.3.0.RELEASE  org.springframework.bootspring-boot-starter-web<!--视图采用freemarker渲染 --> org.springframework.bootspring-boot-starter-freemarker<!-- JDBC --> org.springframework.bootspring-boot-starter-jdbc<!-- mybatis --> org.mybatismybatis-spring1.2.2 org.mybatismybatis3.2.8<!-- mysql --> mysqlmysql-connector-java   maven-compiler-plugin 1.61.6 org.springframework.bootspring-boot-maven-plugin   repackage  org.springframeworkspringloaded1.2.5.RELEASE<!-- 指定最终生成jar包的文件名-->spring-boot

5.创建程序入口Application.java.

 package com.lm.application; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.tomcat.jdbc.pool.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; @EnableAutoConfiguration @SpringBootApplication @ComponentScan(basePackages={"com.lm"})//指定spring管理的bean所在的包 @MapperScan("com.lm.dao")//指定mybatis的mapper接口所在的包 public class Application{ public static void main(String[] args) { Spri<div style="color:transparent">来源gaodai.ma#com搞##代!^码@网</div>ngApplication.run(Application.class, args); } //创建数据源 @Bean @ConfigurationProperties(prefix = "spring.datasource")//指定数据源的前缀 ,在application.properties文件中指定 public DataSource dataSource() { return new DataSource(); } //创建SqlSessionFactory @Bean public SqlSessionFactory sqlSessionFactoryBean() throws Exception { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource()); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*.xml")); return sqlSessionFactoryBean.getObject(); } //创建事物管理器 @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } } 

6.在src/main/resources下建立应用的配置文件application.properties。

 #datasource spring.datasource.url=jdbc:mysql://127.0.0.1:3306/你的数据库名称?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull spring.datasource.username=数据库用户名 spring.datasource.password=数据库密码 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # FREEMARKER (FreeMarkerAutoConfiguration) spring.freemarker.allow-request-override=false spring.freemarker.allow-session-override=false spring.freemarker.cache=true spring.freemarker.charset=UTF-8 spring.freemarker.check-template-location=true spring.freemarker.content-type=text/html spring.freemarker.enabled=true spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=true spring.freemarker.prefer-file-system-access=true spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/ spring.freemarker.settings.template_update_delay=0 spring.freemarker.settings.default_encoding=UTF-8 spring.freemarker.settings.classic_compatible=true spring.freemarker.order=1 #server server.port=80 

相应的配置需要根据自己的实际情况去做修改。

7.在在src/main/resources下创建mybatis目录并在目录下创建UserMapper.xml文件:

    select id, username,password,email from t_user 

8.创建UserController类和视图文件:

 package com.lm.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import com.lm.model.User; import com.lm.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/list") public String list(ModelMap map){ List userList=userService.findAll(); map.addAttribute("userList", userList); return "/user/list"; } } 

可以看出list方法返回的是一个字符串,因为我们给应用加载了freemarker模块做视图展现,所以需要创建一个list模板,模板所在的目录在application.properties中指定为spring.freemarker.template-loader-path=classpath:/templates/,所以我们需要在src/main/resources下创建templates目录,然后在templates下创建user目录,模板文件后缀在application.properties中指定为spring.freemarker.suffix=.ftl,所以最终建立一个list.ftl文件:

   <title>用户列表</title> <table> <tr> <th>id</th><th>用户名</th><th>密码</th><th>邮箱</th></tr><tr> <td>${user.id}</td><td>${user.username}</td><td>${user.password}</td><td>${user.email}</td></tr></table>

模板文件所在位置的目录结构如下图:

9.创建UserService接口:

 package com.lm.service; import java.util.List; import com.lm.model.User; public interface UserService { List findAll(); } 

10.创建UserServiceImpl类实现UserService接口:

 package com.lm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.lm.dao.UserMapper; import com.lm.model.User; import com.lm.service.UserService; @Service public class UserServiceImpl implements UserService{ @Autowired private UserMapper userMapper; @Override public List findAll() { return userMapper.findAll(); } } 

11.创建UserMapper接口:

 package com.lm.dao; import java.util.List; import com.lm.model.User; public interface UserMapper { List findAll(); } 

12.创建实体类User:

 package com.lm.model; public class User { private Integer id; private String username; private String password; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 

13.至此spring-boot框架已搭建完成,然后在Application.java中run as >java application此时在控制台会看到如下日志输出:

14.打开浏览器在地址栏输入http://localhost/user/list便可以看到以下效果:

15.在pom.xml文件上右键Run As>Maven install可将项目打包为jar文件,生成的jar在target目录下,可以将此jar拷贝到服务器上通过”java -jar 最终生成jar包的名字”运行项目。

16.本项目的源码已经上传到,有需要的朋友可以自行下载

以上就是详解MyEclipse中搭建spring-boot+mybatis+freemarker框架的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解MyEclipse中搭建spring-boot+mybatis+freemarker框架

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址